How does the enhanced for statement work for arrays, and how to get an iterator for an array?

后端 未结 13 754
走了就别回头了
走了就别回头了 2020-11-27 04:59

Given the following code snippet:

int[] arr = {1, 2, 3};
for (int i : arr)
    System.out.println(i);

I have the following questions:

13条回答
  •  南方客
    南方客 (楼主)
    2020-11-27 05:37

    How does the above for-each loop work?

    Like many other array features, the JSL mentions arrays explicitly and gives them magical properties. JLS 7 14.14.2:

    EnhancedForStatement:
    
        for ( FormalParameter : Expression ) Statement
    

    [...]

    If the type of Expression is a subtype of Iterable, then the translation is as follows

    [...]

    Otherwise, the Expression necessarily has an array type, T[]. [[ MAGIC! ]]

    Let L1 ... Lm be the (possibly empty) sequence of labels immediately preceding the enhanced for statement.

    The enhanced for statement is equivalent to a basic for statement of the form:

    T[] #a = Expression;
    L1: L2: ... Lm:
    for (int #i = 0; #i < #a.length; #i++) {
        VariableModifiersopt TargetType Identifier = #a[#i];
        Statement
    }
    

    #a and #i are automatically generated identifiers that are distinct from any other identifiers (automatically generated or otherwise) that are in scope at the point where the enhanced for statement occurs.

    Is the array converted to a list to get the iterator?

    Let's javap it up:

    public class ArrayForLoop {
        public static void main(String[] args) {
            int[] arr = {1, 2, 3};
            for (int i : arr)
                System.out.println(i);
        }
    }
    

    then:

    javac ArrayForLoop.java
    javap -v ArrayForLoop
    

    main method with a bit of editing to make it easier to read:

     0: iconst_3
     1: newarray       int
     3: dup
     4: iconst_0
     5: iconst_1
     6: iastore
     7: dup
     8: iconst_1
     9: iconst_2
    10: iastore
    11: dup
    12: iconst_2
    13: iconst_3
    14: iastore
    
    15: astore_1
    16: aload_1
    17: astore_2
    18: aload_2
    19: arraylength
    20: istore_3
    21: iconst_0
    22: istore        4
    
    24: iload         4
    26: iload_3
    27: if_icmpge     50
    30: aload_2
    31: iload         4
    33: iaload
    34: istore        5
    36: getstatic     #2    // Field java/lang/System.out:Ljava/io/PrintStream;
    39: iload         5
    41: invokevirtual #3    // Method java/io/PrintStream.println:(I)V
    44: iinc          4, 1
    47: goto          24
    50: return
    

    Breakdown:

    • 0 to 14: create the array
    • 15 to 22: prepare for the for loop. At 22, store integer 0 from stack into local position 4. THAT is the loop variable.
    • 24 to 47: the loop. The loop variable is retrieved at 31, and incremented at 44. When it equals the array length which is stored in local variable 3 on the check at 27, the loop ends.

    Conclusion: it is the same as doing an explicit for loop with an index variable, no itereators involved.

提交回复
热议问题