declare multidimesional array java code example

Example 1: for loop in multidimensional array java

public class ForLoopExample {
    public static void main(String[] args) {
        int[][] values = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
         
        System.out.println("Elements are :");
        for(int i=0; i< values.length; i++) {
            for(int j=0; j< values[i].length; j++) {
                System.out.print(values[i][j] + "\t");
            }
            System.out.println("");
        }
    }
}

Example 2: 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();
      }
   }
}

Tags:

Cpp Example