I wanted to print the length of the second 10 in field 2d array, but I didn't know how. I want to create another for loop inside of this one and have it count to its length.
我想在字段2d数组中打印第二个10的长度,但我不知道如何。我想在这个内部创建另一个for循环,并将其计入其长度。
public class Main {
public static void main(String[] args) {
Object[][] field = new Object[10][10];
for (int i = 0; i < field.length; i++) {
System.out.println("Length: " + i);
}
System.out.println("Goodbye.");
}
}
4 个解决方案
#1
2
Its kind of two-dimensional array and you can get the length of second array like this field[i].length
and use it in your nested loop.
它是一种二维数组,你可以获得第二个数组的长度,就像这个字段[i] .length一样,并在你的嵌套循环中使用它。
public static void main(String[] args) {
Object[][] field = new Object[10][10];
for (int i = 0; i < field.length; i++) {
System.out.println("Total Rows: " + field.length);
for(int j = 0; j < field[i].length; j++){
System.out.println("Row: " + i);
System.out.println("Length: " + field[i].length);
}
}
System.out.println("Goodbye.");
}
#2
1
field
is a matrix, which is an array of arrays. That means that field[i]
is an array. Specifically, an array of Object
. If you want that array's length, you just call .length
field是一个矩阵,它是一个数组数组。这意味着field [i]是一个数组。具体来说,是一个Object数组。如果你想要那个数组的长度,你只需要调用.length
So the answer is System.out.println("Length: " + field[i].length);
所以答案是System.out.println(“Length:”+ field [i] .length);
#3
0
Another way you could approach this using a nested for loop, as you requested. The solution would look something like this.
您可以根据需要使用嵌套for循环来解决此问题的另一种方法。解决方案看起来像这样。
for( int r = 0; r<field.length; r++ ) {
System.out.println("Length: " + r);
for( int c = 0; c<field[0].length; c++) {
System.out.println("Length: " + c);
}
}
The console messages may not be what you wanted but the header for how long the loop runs would be c<field[0].length;
控制台消息可能不是你想要的,但循环运行多长时间的标题是c
Hope this helps.
希望这可以帮助。
#4
0
field
is an array containing other arrays of 10 length each. Doing field[i]
will get the array at index i
from field
. Therefore using field[i].length
will get the length of the array at index i
. tho count the lengths of all the arrays inside field
you would use:
field是一个包含其他每个长度为10的数组的数组。执行field [i]将从字段获取索引i处的数组。因此,使用field [i] .length将获得索引i处的数组长度。计算你将使用的字段内所有数组的长度:
public class Main {
public static void main(String[] args) {
Object[][] field = new Object[10][10];
for (int i = 0; i < field.length; i++) {
System.out.println("Length: " + field[i].length);
}
System.out.println("Goodbye.");
}
}
#1
2
Its kind of two-dimensional array and you can get the length of second array like this field[i].length
and use it in your nested loop.
它是一种二维数组,你可以获得第二个数组的长度,就像这个字段[i] .length一样,并在你的嵌套循环中使用它。
public static void main(String[] args) {
Object[][] field = new Object[10][10];
for (int i = 0; i < field.length; i++) {
System.out.println("Total Rows: " + field.length);
for(int j = 0; j < field[i].length; j++){
System.out.println("Row: " + i);
System.out.println("Length: " + field[i].length);
}
}
System.out.println("Goodbye.");
}
#2
1
field
is a matrix, which is an array of arrays. That means that field[i]
is an array. Specifically, an array of Object
. If you want that array's length, you just call .length
field是一个矩阵,它是一个数组数组。这意味着field [i]是一个数组。具体来说,是一个Object数组。如果你想要那个数组的长度,你只需要调用.length
So the answer is System.out.println("Length: " + field[i].length);
所以答案是System.out.println(“Length:”+ field [i] .length);
#3
0
Another way you could approach this using a nested for loop, as you requested. The solution would look something like this.
您可以根据需要使用嵌套for循环来解决此问题的另一种方法。解决方案看起来像这样。
for( int r = 0; r<field.length; r++ ) {
System.out.println("Length: " + r);
for( int c = 0; c<field[0].length; c++) {
System.out.println("Length: " + c);
}
}
The console messages may not be what you wanted but the header for how long the loop runs would be c<field[0].length;
控制台消息可能不是你想要的,但循环运行多长时间的标题是c
Hope this helps.
希望这可以帮助。
#4
0
field
is an array containing other arrays of 10 length each. Doing field[i]
will get the array at index i
from field
. Therefore using field[i].length
will get the length of the array at index i
. tho count the lengths of all the arrays inside field
you would use:
field是一个包含其他每个长度为10的数组的数组。执行field [i]将从字段获取索引i处的数组。因此,使用field [i] .length将获得索引i处的数组长度。计算你将使用的字段内所有数组的长度:
public class Main {
public static void main(String[] args) {
Object[][] field = new Object[10][10];
for (int i = 0; i < field.length; i++) {
System.out.println("Length: " + field[i].length);
}
System.out.println("Goodbye.");
}
}