Multi-dimensional array

A multi-dimensional array is nothing but an array of arrays, Where each element of the array is pointing/holding another array.

Syntax to declare

You can declare a multi-dimensional array using three different ways – 

// First way
data-type[][] array_name;

// Second way
data-type array_name[][];

// Third way
data-type[] array_name[];

Declaration example

// First way
int[][] myArray1;

// Second way
int myArray2[][];

// Third way
int[] myArray3[];

Syntax for Instantiation

As we’ve already discussed that arrays in Java are instantiated using the keyword ‘new’ you can do the same for multi-dimensional also.

array_name = new data-type[<row-size>][<column-size>];

Example

Java multi-dimensional array example, try modifying and changing the values.

import java.util.Arrays;

public class MultiDimArray {

    public static void main(String[] args) {

        int[][] myArray = new int[2][2];

        myArray[0][0] = 100;
        myArray[0][1] = 200;

        myArray[1][0] = 50;
        myArray[1][1] = 10;

        // Printing the string representation of the array
        System.out.println(Arrays.deepToString(myArray));

    }
}

You can also declare, instantiate and assign values in a single statement for multi-dimensional array also –

// First way
int[][] myArray1 = new int[][]{{100, 200}, {50, 10}};

// Second way
int[][] myArray2 = {{100, 200}, {50, 10}};

Both the statements are equal and holding the same values, you can choose any one of them to create a multi-dimensional array.