Java 2D and 3D Array

In Java, it is also possible to create an array of arrays known as multidimensional array. For example, we can declare and initialize a 2D array like bellow:

int[][] twoDArray = new int[3][4];

In above example, there is 3 Rows and 2 columns. Look at the Image bellow:

Java 2D Array
Java 2D Array

Check the example code bellow:

public class TwoDiementionalArray {
    public static void main(String[] args) {
        int[][] twoDArray ={{1,2,3,4},{2,4,5,3},{4,4,5,8}, {47,88,95,100},{2,6,8,1}};

        //2D array printing
        System.out.println("====2D Array Print using For Loop====");
        for(int i=0;i<5;i++){
            for(int j=0;j<4;j++){
                System.out.print(twoDArray[i][j]+" ");
            }
            System.out.println();
        }
        //Or print  using enhance for
        System.out.println("====2D Array Print using enhance For Loop====");
        for (int[] rowArray: twoDArray ) {
            for(int data: rowArray) {
                System.out.print(" "+data);
            }
            System.out.println();
        }

    }
}

How to initialize a 2d array in Java?

Here’s an example to initialize a 2d array in Java.

int[][] arr = {
 {1, 2, 3}, 
 {4, 5, 6, 9}, 
 {7}, 
};

As mentioned, each component of array arr is an array in itself, and length of each rows is also different.

Java 2D Array
Java 2D Array

Now Let’s write a program to prove it.

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

      int[][] a = {
            {1, 2, 3}, 
            {4, 5, 6, 9}, 
            {7}, 
      };
      
      System.out.println("Row 1 Length -> " + a[0].length);
      System.out.println("Row 2 Length -> " + a[1].length);
      System.out.println("Row 3 Length -> " + a[2].length);
   }
}

The output will be:

Row 1 Length -> 3
Row 2 Length -> 4
Row 3 Length -> 1

Example: Print all elements of 2d array Using Loop

public class TwoDimentionalArrayEx {
    public static void main(String[] args) {
        // test is a 3d array
        int[][][] test = {
                {
                        {1, -2, 3},
                        {2, 3, 4}
                },
                {
                        {-4, -5, 6, 9},
                        {1},
                        {2, 3}
                }
        };

        // for..each to itterate a  3d array
        for (int[][] array2D: test) {
            for (int[] array1D: array2D) {
                for(int item: array1D) {
                    System.out.print(" "+item);
                }
              System.out.println();
            }
            System.out.println();
        }
    }
}

How to initialize a 3d array in Java?

We can initialize 3d array in similar way. Here’s an example:

/ test is a 3d array
int[][][] test = {
              {
               {1, -2, 3}, 
               {2, 3, 4}
              }, 
              { 
               {-4, -5, 6, 9}, 
               {1}, 
               {2, 3}
              } 
};

Basically, 3d array is an array of 2d arrays.

Similar like 2d arrays, rows of 3d arrays can vary in length.

Now let’s write a program to print elements of 3d array using loop

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

        // 3d array
        int[][][] array3D = {
                {
                        {1, -2, 3},
                        {2, 3, 4}
                },
                {
                        {-4, -5, 6, 9},
                        {1},
                        {2, 3}
                }
        };

        // for..each loop to iterate through elements of 3d array
        for (int[][] array2D: array3D) {
            for (int[] array1D: array2D) {
                for(int element: array1D) {
                    System.out.print(" " +element);
                }
                System.out.println();
            }
            System.out.println();
        }
    }
}

Happy Coding!

Leave a Reply