KLATP
0
Q:

how to get the dimensions of a 2d array in java

for (int row = 0; row < arr.length; row++)//Cycles through rows
{
  for (int col = 0; col < arr[row].length; col++)//Cycles through columns
  {
    System.out.printf("%5d", arr[row][col]); //change the %5d to however much space you want
  }
  System.out.println(); //Makes a new row
}
//This allows you to print the array as matrix
5
public class Main {
  public static void main(String[] args) {
    int[][] test = new int[10][4];
    int rows = test.length;
    int coloumns = test[0].length;
    System.out.println(rows);
    System.out.println(coloumns);
  }
}
4
public static void main(String[] args) {

    int[][] foo = new int[][] {
        new int[] { 1, 2, 3 },
        new int[] { 1, 2, 3, 4},
    };

    System.out.println(foo.length); //2
    System.out.println(foo[0].length); //3
    System.out.println(foo[1].length); //4
}
2

New to Communities?

Join the community