convert single array to multidimensional 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: 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]);
         }
      }
   }
}

Tags:

Cpp Example