2d array row column code example

Example 1: Index through 2d array

int[][]x;//define
for(int i=0;i<x.length;i++){
  for(int j=0;j<x[i].length;j++){
    //dp something with x[i][j]
  }
}

Example 2: Index through 2d array

int[][]x;//define
for(int []y:x){
  for(int a:y){
    //do something with a
  }
}

Example 3: 2d array java

//Length
int[][]arr= new int [filas][columnas];
arr.length=filas;

        int[][] a = {
            {1, 2, 3}, 
            {4, 5, 6, 9}, 
            {7}, 
        };
      
        // calculate the length of each row
        System.out.println("Length of row 1: " + a[0].length);
        System.out.println("Length of row 2: " + a[1].length);
        System.out.println("Length of row 3: " + a[2].length);
    }

Tags:

Cpp Example