How does primitive array work with new for each loop in Java?

后端 未结 7 706
粉色の甜心
粉色の甜心 2020-12-02 01:41

I understand that new for each loop works with Iterable and arrays, but I don\'t know what goes behind the scenes when working with arrays.

Can anyone help me under

相关标签:
7条回答
  • 2020-12-02 02:26

    A bit late, but here it is.

    The compiler knows if you are using the for-each loop statement for a collection or for an array.

    If used for collection, the compiler translates the for-each loop to the equivalent for loop using an Iterator.

    If used for an array, the compiler translates the for-each loop to the equivalent for loop using an index variable.

    Here is a description at oracle.com

    0 讨论(0)
提交回复
热议问题