简体   繁体   中英

Why the left side size in for each is not equal to the right side?

The declared size in for-each is always less than the size of the array.why ?

int[][][] c = {{{1,2,3}}};
for(int [][] z : c )
    //code

c is a 3D array so should z, right? like:

int[][][] c = {{{1,2,3}}};
for (int[][][] z : c) // which is syntactically wrong
    //code

Thank you in advance.

The answer as I understand it from the comments and the below answer: the declaration inside for-each

in the 3D array for-each considers every 2D array as an element,

in the 2D array for-each considers every 1D array as an element and so on..

in the 1D array it takes the elements of the array directly as elements.

Read that construct as the for-each loop. In this case, for each int[][] in the int[][][] . Another example, for-each int in an int[] .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM