How to get the rows and columns of a matrix (2D array) in Java

1 Answer

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


 
 
/*
run:
 
4
5
 
*/

 



answered Feb 19, 2021 by avibootz

Related questions

1 answer 186 views
1 answer 179 views
2 answers 250 views
1 answer 235 views
1 answer 265 views
...