在java中查找二维数组的维度

Pav*_*lli 5 java multidimensional-array

我正在使用 Java 中的数组并有这个疑问。java中如何求二维数组的维数?例如,我从 System.in 获取数组输入并将其传递到另一个方法中,如下所示:

Scanner in = new Scanner(System.in);
int arr[][] = new int[6][6];
for(int i=0; i < 6; i++){
    for(int j=0; j < 6; j++){
                arr[i][j] = in.nextInt();
        }
}
findSize(arr);
/*
*
*Other code
*
*/
findSize(int[] inputArr){
//I want to find the dimensions of the array here
}
Run Code Online (Sandbox Code Playgroud)

数组的两个维度都大于 0。感谢您的帮助。

ΦXo*_*a ツ 6

这个方法:

findSize(int[] inputArr){
//I want to find the dimensions of the array here
}
Run Code Online (Sandbox Code Playgroud)

正在获取一个二维数组作为参数

因此你应该这样做:

findSize(int[][] inputArr){
     int heiht = inputArr.length;
     int width = inputArr[0].length;
}
Run Code Online (Sandbox Code Playgroud)


Pav*_*lli 3

我只需要像这样访问数组的第 0 个元素:

int size = inputArr[0].length;
Run Code Online (Sandbox Code Playgroud)

这样就可以解决问题了!