[英]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 ? for-each
声明的大小总是小于数组的大小。为什么?
int[][][] c = {{{1,2,3}}};
for(int [][] z : c )
//code
c is a 3D array so should z, right? c 是一个 3D 数组,所以 z 应该是,对吗? 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
我从评论和以下答案中理解的答案:
for-each
内部的声明
in the 3D array for-each
considers every 2D array as an element,在 3D 数组中
for-each
将每个 2D 数组视为一个元素,
in the 2D array for-each
considers every 1D array as an element and so on..在二维数组中
for-each
将每个一维数组视为一个元素,依此类推。
in the 1D array it takes the elements of the array directly as elements.在一维数组中,它直接将数组的元素作为元素。
Read that construct as the for-each
loop.将该构造读作
for-each
循环。 In this case, for each int[][]
in the int[][][]
.在这种情况下,对于
int[][]
中的每个int[][]
int[][][]
。 Another example, for-each
int
in an int[]
.另一个例子,
for-each
int
in an int[]
。
int[] arr = {1,2,3};
for (int i : arr) {
System.out.println(i);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.