Java Multidimensional Arrays



In this tutorial, we will learn about the Java Multidimensional array. We will see how to declare, initialize, and work with 2-dimensional arrays and 3-dimensional arrays with the help of examples.

Before we learn about the multidimensional array, make sure to learn about Jara array.


Multidimensional Arrays

A multidimensional array is an array of arrays. Each element of a multidimensional array is another array.

In the following example, we will create a multidimensional array named m. It's a 2-dimensional array of 3 * 5 that can hold a maximum of 3 * 5 = 15 elements.

int[][] m = new int[3][5];

The elements in a multidimensional array are represented in rows and columns.

2-dimensional Array

Java uses zero-based indexing, so indexing of arrays in Java starts with 0 and not 1.

Let us take another example of the multidimensional array. In the following example, we will create a 3-dimensional array.

String[][][] d = new String[2][4][3];

Here above, d is a 3d array that can hold a maximum of 2 * 4 * 3 = 24


How to initialize a 2-dimensional array in Java?

We can initialize a 2-dimensional array in Java as follows.

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

As we can see above, each element of the multidimensional array is an array itself. Unlike C/C++ in Java, each row of the multidimensional array can be of different lengths.

Initialization of 2-dimensional Array

Example of a 2-dimensional Array

In the following example, we will create a multidimensional array named m. Since each component of a multidimensional array is also an array (m[0], m[1], and m[2] are also arrays).

class TwoDimensionalArray{
    public static void main(String[] args) {
        // 2d array 
        int[][] m = {
            {1, 3, 2},
            {4, 6, 5, 8, 10},
            {9, 7}
        };

        // showing the length of each row
        System.out.println("Length of row 1: " + m[0].length);
        System.out.println("Length of row 1: " + m[1].length);
        System.out.println("Length of row 1: " + m[2].length);
    }
}

Output:

Length of row 1: 3
Length of row 1: 5
Length of row 1: 2

Here above, we are using the length attribute to calculate the length of each row.


How to access a 2-dimensional Array in Java?

To access elements of a 2-dimensional array, specify two indexes: one for the array, and one for the element inside that array.

In the following example, we will access the second element in the third array of the m multidimensional array.

int[][] m = {
    {1, 2},
    {3, 4, 5, 6},
    {7, 8}
};

int x = m[2][1];
System.out.println("The second element in the third array of 'm' is " + x);

Output:

The second element in the third array of 'm' is 8

Example: Printing all elements of a 2-dimensional array Using Loop

In the following example, we will print all elements of a 2-dimensional using a for loop.

class TwoDimensionalArray{
    public static void main(String[] args) {
        int[][] m = {
            {1, 3},
            {4, 6, 5},
            {9, 7}
        };
    
        for (int i = 0; i < m.length; ++i) {
            for (int j = 0; j < m[i].length; ++j) {
                System.out.println(m[i][j]);
            }
        }
      }
}

Output:

1
3
4
6
5
9
7

We can also use a for-each loop to access elements of the multidimensional array.

class twoDimensionalArray{
    public static void main(String[] args) {
        int[][] m = {
            {1, 3},
            {4, 6, 5},
            {9, 7}
        };

        // first for...each loop access the individual array
        for (int[] innerArray: m) {
            // second for...each loop access each element inside the row
            for (int data: innerArray) {
                System.out.println(data);
            }
        }
    }
}

Output:

1
3
4
6
5
9
7

In the above example, we have created a 2-dimensional array named m. Then we used a for...each loop inside another one to access the element of the array.


How to initialize a 3-dimensional array in Java?

We can initialize a 3-dimensional array similar to the 2-dimensional array. For example,

int[][][] three_dim = {
    {
        {1, -3, 4},
        {4, 5}
    },
    {
        {-3, 9, 6},
        {1, 2},
        {7, 8, 9}
    }
}

Basically, a 3-dimensional array is an array of 2-dimensional arrays. The rows of a 3-dimensional array can also vary in length, just like a 2-dimensional array.


Example of 3-dimensional Array

class ThreeDimensionalArray {
    public static void main(String[] args) {
        
        // create a 3d array
        int[][][] three_dim = {
            {
                {1, -3, 4},
                {8, 5}
            },
            {
                {-3, 9},
                {1, 2},
                {9}
            }
        };

        // for...each loop to iterate through elements of 3-dimensional array 
        for(int[][] arr2d: three_dim) {
            for(int[] arr1d: arr2d) {
                for(int item: arr1d) {
                    System.out.println(item);
                }
            }
        }
    }
}

Output:

1
-3
4
8
5
-3
9
1
2 
9


ExpectoCode is optimized for learning. Tutorials and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 2020-2021 by ExpectoCode. All Rights Reserved.