[英]Finding 2D array length?
I know that array.length returns the number of rows, but I'm looking for the length of each row. 我知道array.length返回行数,但是我正在寻找每行的长度。 Apparently array[0].length doesnt do it.
显然array [0] .length不能做到这一点。 Any ideas?
有任何想法吗?
jcomeau@intrepid:~/rentacoder$ cat /tmp/test.java
public class test {
public static void main(String[] args) {
int[][] blah = {{1, 2}, {3, 4, 5}};
System.out.println("lengths: " + blah.length + ", " + blah[0].length +
", " + blah[1].length);
}
}
jcomeau@intrepid:~/rentacoder$ (cd /tmp && java test)
lengths: 2, 2, 3
Seems to work. 似乎可以工作。
In Java there are only one-dimensional arrays internally. 在Java中,内部只有一维数组。
A two-dimensional array is an array of arrays, so the array for the first dimension and each of the arrays for the second dimension may have a different size. 二维数组是数组的数组,因此用于第一维的数组和用于第二维的每个数组都可以具有不同的大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.