Java 如何获得二维数组中行和列的长度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19343521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get length of rows and columns in Two-Dimensional array?
提问by Samiey Mehdi
I have a 2D array like following:
我有一个二维数组,如下所示:
int[][] array=
{
{2,3,3},
{5,6,66}
};
I want get length of rows and columns:
我想获得行和列的长度:
int rows= // get row length of array
int columns= // get column length of array
Thanks!
谢谢!
采纳答案by T.J. Crowder
It's important to understand that Java doesn't really have two-dimensional arrays. It has arrays of arrays. That means, for instance, that you can have this:
重要的是要了解 Java 并没有真正的二维数组。它有数组的数组。这意味着,例如,您可以拥有以下内容:
int[][] array=
{
{1},
{1, 2, 3},
{1, 2, 3, 4, 5},
{1, 2}
};
So there is no oneupper bound of the second level. Java arrays are inherently jagged, each of the second level in the above has its own length.
所以第二层没有一个上界。Java 数组本质上是锯齿状的,上面的每个第二级都有自己的长度。
So to loop them correctly, you have to check for each of the second-level arrays:
因此,要正确循环它们,您必须检查每个二级数组:
int x, y;
int[] second;
for (x = 0; x < array.length; ++x) {
second = array[x];
for (y = 0; y < second.length; ++y) {
// ....
}
}
Full example: Live Copy
完整示例:Live Copy
public class ArrayExample {
public static void main(String[] args) {
int[][] array=
{
{1},
{1, 2, 3},
{1, 2, 3, 4, 5},
{1, 2}
};
int x, y;
int[] second;
for (x = 0; x < array.length; ++x) {
second = array[x];
for (y = 0; y < second.length; ++y) {
System.out.println(x + "," + y + ": " + second[y]);
}
System.out.println();
}
}
}
Output:
输出:
0,0: 1 1,0: 1 1,1: 2 1,2: 3 2,0: 1 2,1: 2 2,2: 3 2,3: 4 2,4: 5 3,0: 1 3,1: 2
Or if you don't need the indexes, just the values, you can use the enhanced for
loop: Live Example
或者,如果您不需要索引,只需要值,则可以使用增强for
循环:Live Example
public class ArrayExample {
public static void main(String[] args) {
int[][] array=
{
{1},
{1, 2, 3},
{1, 2, 3, 4, 5},
{1, 2}
};
for (int[] second : array) {
for (int entry : second) {
System.out.println(entry);
}
System.out.println();
}
}
}
Output:
输出:
1 1 2 3 1 2 3 4 5 1 2
回答by Maroun
In order to better understand this, take a look at this image:
为了更好地理解这一点,请看一下这张图片:
This image is what you call 2D array, as you can see, it's actually an array of arrays.
这个图像就是你所说的2D array,正如你所看到的,它实际上是一个arrays 数组。
nums.length
will return the length of the blue array (which is the number of the rows).
Now if you want to get the number of columns, you should access one row by nums[0]
for example, and then do nums[0].length
, which will yield 4.
nums.length
将返回蓝色数组的长度(即行数)。
现在,如果您想获取列数,您应该访问一行nums[0]
,例如,然后执行nums[0].length
,这将产生 4。
Now, simply replace nums
with array
...
现在,只需替换nums
为array
...
Note: As you see in the image, the number of columns might differ and it doesn't have to be the same for each row.
注意:如您在图像中看到的,列数可能不同,并且每一行不必相同。
回答by nullptr
Try this
尝试这个
int upperBound0 = array.length - 1; // = 1
int upperBound1 = array[0].length - 1; // = 2
As you commented:
正如你评论的那样:
upper bound is maximum index of rows and columns
上限是行和列的最大索引
回答by JWqvist
In a 2D array there is an array in every place in the first array so you can number of columns by getting the 2D arrays lenght then to get the size of each column you have to go to there place in the 2D array and get the lenght
在二维数组中,第一个数组中的每个位置都有一个数组,因此您可以通过获取二维数组长度来获得列数,然后获取每列的大小,您必须到二维数组中的位置并获取长度
回答by codertoaster
This is how I do it. Suppose a is any 2D array where length of all rows is different.
我就是这样做的。假设 a 是所有行的长度不同的任何二维数组。
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a[i].length;j++)
{
System.out.print(a[i][j]);
}
}