英文:
Why the left side size in for each is not equal to the right side?
问题
在 for-each
中声明的大小总是小于数组的大小。为什么?
int[][][] c = {{{1,2,3}}};
for(int [][] z : c )
//code
c 是一个三维数组,所以 z 也应该是三维的,对吗?
就像:
int[][][] c = {{{1,2,3}}};
for (int[][][] z : c) // 这在语法上是错误的
//code
提前感谢您的回答。
根据我从评论和以下答案中理解的,答案是:在三维数组的 for-each
中,它将每个二维数组视为一个元素,在二维数组的 for-each
中,它将每个一维数组视为一个元素,依此类推,在一维数组中,它直接将数组的元素视为元素。
英文:
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.
答案1
得分: 0
阅读该结构为“for-each”循环。在这种情况下,对于int[][][]
中的每个int[][]
。另一个例子是,在int[]
中的每个int
上使用for-each
。
int[] arr = {1,2,3};
for (int i : arr) {
System.out.println(i);
}
英文:
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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论