获得的二维阵列的长度 [英] Getting the length of two-dimensional array
问题描述
我如何得到一个数组的第二维,如果我不知道吗? array.length
只给出了第一个维度。
How do I get the second dimension of an array if I don't know it? array.length
gives only the first dimension.
例如,在
public class B {
public static void main(String [] main){
int [] [] nir = new int [2] [3];
System.out.println(nir.length);
}
}
我将如何获得 NIR
的第二个方面,也就是3的值
how would I get the value of the second dimension of nir
, which is 3.
感谢
推荐答案
的这 3
您已经创建了一个多维数组。 NIR
为int数组的数组;你有长度为3的两个数组。
You've created a multi-dimentional array. nir
is an array of int arrays; you've got two arrays of length three.
System.out.println(nir[0].length);
会给你你的第一个数组的长度。
would give you the length of your first array.
另外值得一提的是,你不必初始化像你一样,这意味着所有的阵列不必是相同长度的多维数组(或存在的话)。
Also worth noting is that you don't have to initialize a multi-dimensional array as you did, which means all the arrays don't have to be the same length (or exist at all).
int nir[][] = new int[5][];
nir[0] = new int[5];
nir[1] = new int[3];
System.out.println(nir[0].length); // 5
System.out.println(nir[1].length); // 3
System.out.println(nir[2].length); // Null pointer exception
这篇关于获得的二维阵列的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!