two dimensional array java code example

Example 1: Multidimensional array in java

// two dimensional string array in java example
public class TwoDimensionalStringArray 
{
   public static void main(String[] args) 
   {
      String[][] animals = {
              { "Lion", "Tiger", "Cheetah" },
              { "Deer", "Jackal", "Bear" },
              { "Hyena", "Fox", "Elephant" } };
      for(int a = 0; a < animals.length; a++) 
      {
         System.out.print(animals[a][0] + " ");
         for(int b = 1; b < animals[a].length; b++) 
         {
            System.out.print(animals[a][b] + " ");
         }
         System.out.println();
      }
   }
}

Example 2: 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 3: 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 4: Multidimensional array in java

// create dynamic two dimensional array in java
import java.util.ArrayList;
import java.util.List;
public class Dynamic2dArray 
{
   public static void main(String[] args) 
   {
      List<int[]> li = new ArrayList<>();
      li.add(new int[]{2,4,6});
      li.add(new int[]{3,5});
      li.add(new int[]{1});
      // element at row 0, column 0
      System.out.println("Element at [0][0]: " + li.get(0)[1]);
      // get element at row : 1, column : 1
      System.out.println("Element at [1][1]: " + li.get(1)[1]);
   }
}

Example 5: Multidimensional array in java

// java two dimensional array length example
public class TwoDimensionalArrayLength
{
   public static void main(String[] args) 
   {
      int[][] arrNumber = { 
              { 1, 3, 5 },
              { 7, 9, 2 },
              { 1, 2, 3, 4, 5 } };
      System.out.println("Java two dimensional array length: " + arrNumber.length);
   }
}

Example 6: Multidimensional array in java

// how to fill a 2d array java
public class Fill2dArrayExample
{
   public static void main(String[] args) 
   {
      int[][] arrNumbers = new int[3][3];
      for(int a = 0; a < 3; a++)
      {
         for(int b = 0; b < 3; b++)
         {
            arrNumbers[a][b] = (int) (Math.random() * 6) ;
         }
      }
      for(int x = 0; x < 3; x++)
      {
         for(int y = 0; y < 3; y++)
         {
            System.out.print(arrNumbers[x][y] + " ");
         }
         System.out.println();
      }
   }
}

Tags:

Cpp Example