Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the dimensions of a 2D array in java

I'm playing around with Arrays in Java and had this doubt. How do I find the dimensions of a 2D array in java? For example, I get an array input from System.in and pass it in another method like this:

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
}

Both dimensions of the array are greater than 0. Appreciate the help.

like image 841
Pavan Kottapalli Avatar asked Dec 25 '22 07:12

Pavan Kottapalli


1 Answers

This method:

findSize(int[] inputArr){
//I want to find the dimensions of the array here
}

is getting as parameter a 2 dimentional array

hence you should do:

findSize(int[][] inputArr){
     int heiht = inputArr.length;
     int width = inputArr[0].length;
}
like image 189
ΦXocę 웃 Пepeúpa ツ Avatar answered Jan 10 '23 12:01

ΦXocę 웃 Пepeúpa ツ