Java Multidimensional Arrays

Summary: in this tutorial, you will learn about Java multidimensional arrays focusing on 2-dimensional (2D) arrays.

Introduction to Java multidimensional arrays

In the array tutorial, you have learned how to define an array that holds multiple values. For example:

int numbers = {1, 2, 3};Code language: Java (java)

In this example, the numbers array has one dimension.

Java allows you to define an array with more than one dimension. When an array has two or more dimensions, it is called a multidimensional array.

Technically, a multidimensional array is an array of arrays, where each element is an array.

Declaring multidimensional arrays

To define a multidimensional array in Java, you specify the data type, followed by one or more pairs of square brackets [] to indicate dimensions and the array name.

For example, the syntax for declaring a 2D array is as follows:

dataType [][] arrayName;Code language: Java (java)

The following illustrates how to declare a 2D array of integers:

int [][] matrix;Code language: Java (java)

In this example, the matrix is a 2D array because it has two square brackets.

To define a 3D array, you use three square brackets:

int [][][] cube;Code language: Java (java)

Initializing multidimensional arrays

To initialize a multidimensional array with predefined values in the array’s declaration, you use nested curly braces that represent the values for each dimension:

For example, the following illustrates how to initialize elements of the matrix array:

int[][] matrix = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};Code language: Java (java)

Accessing elements

Accessing elements in a 2D array requires the row and column indices:

int element = arrayName[rowIndex][columnIndex];Code language: Java (java)

For example, the following accesses the element in the second row and third column:

int element = matrix[1][2]; // 6Code language: Java (java)

Iterating through multidimensional arrays

To iterate elements of a multidimensional array, you can use a nested for loop:

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

        // Iterating elements
        for (int i = 0; i < matrix.length; i++) {        // Rows
            for (int j = 0; j < matrix[i].length; j++) { // Columns
                int element = matrix[i][j];
                System.out.println(element);
            }
        }
    }
}Code language: Java (java)

Java 2D array example

The following example illustrates how to add two matrixes using 2D arrays:

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

        int[][] matrixA = {
            {1, 2},
            {3, 4}
        };

        int[][] matrixB = {
            {5, 6},
            {7, 8}
        };

        int numRows = matrixA.length;
        int numColumns = matrixA[0].length;

        // create the result array
        int[][] result = new int[numRows][numColumns];

        // add elements to the result array
        for (int i = 0; i < numRows; i++) {
            for (int j = 0; j < numColumns; j++) {
                result[i][j] = matrixA[i][j] + matrixB[i][j];
            }
        }


        // display the result array
        for (int i = 0; i < numRows; i++) {
            for (int j = 0; j < numColumns; j++) {
                System.out.print(result[i][j] + "\t");
            }
            System.out.println();
        }

    }
}Code language: Java (java)

Output:

6	8	
10	12Code language: Java (java)

Summary

  • Use an array of arrays to define a multidimensional array.
  • The number of square brackets determines the dimension of a multidimensional array.
  • Use a nested for loop to iterate over elements of a multidimensional array.