#眉標=JDK 5.0 #副標=JDK 5.0 Tiger Enhanced for loop #大標=剖析JDK 5.0 Enhanced for loop #作者=文/陳綮紳 =====================程式==================== 程式1 public void testForLoop(int[] value) { int valueLength = value.length; for( int i = 0 ; i < valueLength ; i++) { System.out.println(value[i]); } } =====================程式==================== ===========圖========== 圖1 testForLoop方法的位元碼。 ===========圖========== ===========圖========== 圖2 testForLoop方法的LocalVariableTable。 ===========圖========== =====================程式==================== 程式2 public void testForInLoop(int[] value) { for( int i : value) { System.out.println(i); } } =====================程式==================== =====================程式==================== 程式3 public void testForInLoop(int[] value) { int[] arr$ = value; int len$ = arr$.length; for(int i$ = 0; i$ < len$; i$++) { int i = arr$[i$]; System.out.println(i); } } =====================程式==================== =====================程式==================== 程式4 public void testForLoopCollection(Collection c) { for(Iterator i = c.iterator() ; i.hasNext() ; ) { Integer value = (Integer) i.next(); System.out.println(value); } } =====================程式==================== =====================程式==================== 程式5 public void testForInCollection(Collection c) { for(Object i : c) { Integer value = (Integer) i; System.out.println(value); } } =====================程式==================== =====================程式==================== 程式6 public void testForInCollection(Collection c) { for(Iterator i$ = c.iterator ; i$.hasNext() ; ) { Object i = i$.next(); Integer value = (Integer)i; System.out.println(value); } } =====================程式==================== =====================程式==================== 程式7 public void testForInGeneric(Collection c) { for(Integer i : c) System.out.println(i); } =====================程式==================== =====================程式==================== 程式8 public void test2DArrayForIn() { int[][] value = new int[][]{{1,2},{3,4}}; for(int[] v : value) for(int i : v) System.out.println(i); } =====================程式====================