java multi dimensional arrays code example

Example 1: java two dimensional arrays

int[][] multiples = new int[4][2];     // 2D integer array with 4 rows 
                                          and 2 columns
String[][] cities = new String[3][3];  // 2D String array with 3 rows 
                                          and 3 columns

Example 2: Multidimensional array in java

// two dimensional array in java example program
public class SimpleTwodimensionalArray
{
   public static void main(String[] args) 
   {
      // declare and initialize two dimensional array
      int[][] arrnumbers = {{2, 4},{6, 8},{10, 12}};   
      System.out.println("Two dimensional array in java: ");    
      for(int a = 0; a < 3; a++) 
      {
         for(int b = 0; b < 2; b++) 
         {
            System.out.println(arrnumbers[a][b]);
         }
      }
   }
}

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);
    }

Example 4: multidimensional arrays java

ships: [
		{ locations: [0, 0, 0], hits: ["", "", ""] },
		{ locations: [0, 0, 0], hits: ["", "", ""] },
		{ locations: [0, 0, 0], hits: ["", "", ""] }
	],

Tags:

Java Example