I would like to use a multidimensional array to store a grid of data. However, I have not found a simple way to find the length of the 2nd part of the array. For example:
我想使用多维数组来存储数据网格。但是,我还没有找到一种简单的方法来查找数组第二部分的长度。例如:
boolean[][] array = new boolean[3][5];
System.out.println(array.length);
will only output 3.
只输出3。
Is there another simple command to find the length of the second array? (i.e. output 5 in the same manner)
是否有另一个简单的命令来查找第二个数组的长度? (即以相同的方式输出5)
5 个解决方案
#1
9
Try using array[0].length
, this will give the dimension you're looking for (since your array is not jagged).
尝试使用array [0] .length,这将给出你正在寻找的维度(因为你的数组没有锯齿状)。
#2
7
boolean[][] array = new boolean[3][5];
Creates an array of three arrays (5 boolean
s each). In Java multidimensional arrays are just arrays of arrays:
创建一个包含三个数组的数组(每个数组为5个布尔值)。在Java中,多维数组只是数组的数组:
array.length
gives you the length of the "outer" array (3 in this case).
给出“外部”数组的长度(在这种情况下为3)。
array[0].length
gives you the length of the first "inner" array (5 in this case).
给出第一个“内部”数组的长度(在这种情况下为5)。
array[1].length
and
和
array[2].length
will also give you 5, since in this case, all three "inner" arrays, array[0]
, array[1]
, and array[2]
are all the same length.
也会给你5,因为在这种情况下,所有三个“内部”数组,数组[0],数组[1]和数组[2]都是相同的长度。
#3
1
array[0].length would give you 5
array [0] .length会给你5
#4
1
int a = array.length;
if (a > 0) {
int b = array[a - 1].length;
}
should do the trick, in your case a would be 3, b 5
应该做的伎俩,在你的情况下,将是3,b 5
#5
0
You want to get the length of the inner array in a 3 dimensional array
您希望在三维数组中获取内部数组的长度
e.g.
例如
int ia[][][] = new ia [4][3][5];
System.out.print(ia.length);//prints 4
System.out.print(ia[0].length);//prints 3
System.out.print(ia[0].[0].length); // prints 5 the inner array in a three D array
By induction: For a four dimensional array it's:
通过归纳:对于四维数组,它是:
ia[0].[0].[0].length
......
......
#1
9
Try using array[0].length
, this will give the dimension you're looking for (since your array is not jagged).
尝试使用array [0] .length,这将给出你正在寻找的维度(因为你的数组没有锯齿状)。
#2
7
boolean[][] array = new boolean[3][5];
Creates an array of three arrays (5 boolean
s each). In Java multidimensional arrays are just arrays of arrays:
创建一个包含三个数组的数组(每个数组为5个布尔值)。在Java中,多维数组只是数组的数组:
array.length
gives you the length of the "outer" array (3 in this case).
给出“外部”数组的长度(在这种情况下为3)。
array[0].length
gives you the length of the first "inner" array (5 in this case).
给出第一个“内部”数组的长度(在这种情况下为5)。
array[1].length
and
和
array[2].length
will also give you 5, since in this case, all three "inner" arrays, array[0]
, array[1]
, and array[2]
are all the same length.
也会给你5,因为在这种情况下,所有三个“内部”数组,数组[0],数组[1]和数组[2]都是相同的长度。
#3
1
array[0].length would give you 5
array [0] .length会给你5
#4
1
int a = array.length;
if (a > 0) {
int b = array[a - 1].length;
}
should do the trick, in your case a would be 3, b 5
应该做的伎俩,在你的情况下,将是3,b 5
#5
0
You want to get the length of the inner array in a 3 dimensional array
您希望在三维数组中获取内部数组的长度
e.g.
例如
int ia[][][] = new ia [4][3][5];
System.out.print(ia.length);//prints 4
System.out.print(ia[0].length);//prints 3
System.out.print(ia[0].[0].length); // prints 5 the inner array in a three D array
By induction: For a four dimensional array it's:
通过归纳:对于四维数组,它是:
ia[0].[0].[0].length
......
......