How to get the dimensions of a 2D array in Java

2 Answers

0 votes
public class MyClass {
    public static void main(String args[]) {
        int arr[][] = {{1,2,3,4,-1},{5,6,7,8,-4},{9,10,11,12,-9},{13, 14, 15, 16,-2}};    
  
        int rows = arr.length; 
        int cols = arr[0].length;  
         
        System.out.println(rows);
        System.out.println(cols);
    }
}
 
 
  
  
/*
run:
  
4
5
  
*/

 



answered Mar 30, 2021 by avibootz
0 votes
public class MyClass {
    public static void main(String args[]) {
        int[][] arr = new int[6][4];
  
        int rows = arr.length; 
        int cols = arr[0].length;  
         
        System.out.println(rows);
        System.out.println(cols);
    }
}
 
 
  
  
/*
run:
  
6
4
  
*/

 



answered Mar 30, 2021 by avibootz

Related questions

...