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

后端 未结 7 715
粉色の甜心
粉色の甜心 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

提交回复
热议问题