How to get matrix rows and columns (cols) in Java

1 Answer

0 votes
public class MyClass {
    public static void main(String args[]) {
        int[][] matrix = {
	        {2, 0, 5, 9},
	        {0, 4, 0, 3},
	        {0, 8, 0, 1}
        };
        
        int rows = matrix.length;
        int cols = matrix[0].length;

        System.out.println("rows: " + rows);
        System.out.println("cols: " + cols);
    }
}




/*
run:
  
rows: 3
cols: 4

*/



 



answered Nov 19, 2022 by avibootz

Related questions

1 answer 185 views
1 answer 102 views
2 answers 135 views
1 answer 219 views
2 answers 402 views
1 answer 147 views
...