one dimensional array in 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

// 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 4: two dimensional array in java example program

class MultidimensionalArray {
    public static void main(String[] args) {

        int[][] a = {
            {1, -2, 3}, 
            {-4, -5, 6, 9}, 
            {7}, 
        };
      
        for (int i = 0; i < a.length; ++i) {
            for(int j = 0; j < a[i].length; ++j) {
                System.out.println(a[i][j]);
            }
        }
    }
}

Example 5: how to initialize one dimensional array in java

int[] a; // valid declaration
int b[]; // valid declaration 
int[] c; // valid declaration

Tags:

Cpp Example