[英]For each loop using 2D array
This is the snippet of Java code:这是 Java 代码的片段:
int[][] uu = new int[1][1];
uu[0][0] = 5;
for(int[] u: uu){
System.out.println(u[0]);
}
It prints 5. But why does the declaration part of for loop is declared as int[] u
, but not as int[][] u
?它打印 5。但是为什么 for 循环的声明部分被声明as int[] u
,而不是as int[][] u
?
At the uu you reference 2D array... That is not a homework.在 uu 你引用 2D 数组......那不是作业。 I am preparing for Java certification.我正在准备Java认证。 Cheers干杯
Since your uu
is an array of array
. 因为你的uu
是一个array of array
。 So, when you iterate over it, you will first get an array
, and then you can iterate over that array to get individual elements. 因此,当您迭代它时,您将首先获得一个array
,然后您可以迭代该数组以获取单个元素。
So, your outer loop has int[]
as type, and hence that declaration. 所以,你的外部循环有int[]
作为类型,因此声明。 If you iterate through your u
in one more inner loop, you will get the type int
: - 如果你在另一个内部循环中遍历你的u
,你会得到int
类型: -
for (int[] u: uu) {
for (int elem: u) {
// Your individual element
}
}
It is because uu
is an array of int[]
arrays. 这是因为uu
是一个int[]
数组的数组。 So every item in it is int[]
. 所以其中的每一项都是int[]
。 In a for
loop you declare the type of an item in an array you iterate over. 在for
循环中,您声明迭代的数组中项的类型。
The loop is iterating on the elements of uu
, which are objects of type int[]
. 循环迭代uu
的元素,它们是int[]
类型的对象。 (Or in other words - u
is an element in uu
, thus it is an int[]
). (换句话说 - u
是uu
的元素,因此它是int[]
)。
The declaration is always of the type of the objects retrieved by the iteration - in this case - it is int[]
- 声明始终是迭代检索的对象的类型 - 在这种情况下 - 它是int[]
-
Same as iterating over an int[]
is: 与迭代int[]
是:
for (int x : myArray) { ...}
because each element of x
is of type int
. 因为x
每个元素都是int
类型。
"why does the declaration part of for loop is declared as int[] u, but not as int[][] u?" “为什么for循环的声明部分被声明为int [] u,而不是int [] [] u?”
The array is two-dimensional, so you are dealing with a double-layered iteration. 该数组是二维的,因此您正在处理双层迭代。 You have an array "inside" another, in the same principle as List<List<Integer>>
would work. 你有一个“内部”数组,其原理与List<List<Integer>>
相同。
To iterate through all the elements, you should consider a rows-elements structure. 要遍历所有元素,您应该考虑行元素结构。 It's necessary that you get each row from the container, and then each element from each row. 您必须从容器中获取每一行,然后从每行获取每个元素。
for(int[] u: uu)
is simply a for-each iteration rows, with the same principle of for(int row = 0; row < container.length; row++)
, and u
or respectively container[row]
are not elements themselves, but rows (arrays) of elements. for(int[] u: uu)
只是for-each迭代行,其原理与for(int row = 0; row < container.length; row++)
,而u
或者container[row]
都不是元素他们自己,但元素的行(数组)。 Meaning you require a second iteration layer to get the elements: 这意味着您需要第二个迭代层来获取元素:
int[][] container = new int[10][10];
//... - Fill elements.
for(int row = 0; row < container.length; row++){
for(int element = 0; element < container[row].length; element++){
System.out.printf("Row: %d Element: %d Value: %d\n", row, element, container[row][element]);
}
}
This is an example of finding the sum in 2d array and print it这是在二维数组中查找总和并打印它的示例
public class Array2DForEach {
public static void main(String[] args) {
int sum = 0;
int[][] myFirst2DArray = {
{ 3, 5, 1, 9 },
{ 10, 15, 3, 0 },
{ 1, 11, 31, 90 },
{ 2, 51, 1, 9 }
};
for (int[] row:myFirst2DArray) {
for (int columnElement : row) {
sum+=columnElement;
}
}
System.out.println(sum);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.