且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

获得的二维阵列的长度

更新时间:2023-02-26 15:15:06

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