why is the enhanced for loop more efficient than the normal for loop

后端 未结 7 1764
一整个雨季
一整个雨季 2020-12-01 00:35

I read that the enhanced for loop is more efficient than the normal for loop here:

http://developer.android.com/guide/practices/performance.html#fo

7条回答
  •  再見小時候
    2020-12-01 01:04

    The for-each uses the Iterator interface. I doubt that it is more efficient than the "old" style. The Iterator also needs to check the size of the list.

    It's mostly for readability.

    It should be faster for non-random-access collections like LinkedList, but then the comparison is unfair. You would not have used to second implementation (with slow indexed access) anyway.

提交回复
热议问题