Skip to content
On this page

Multidimensional Array

two-dimensional array

A two-dimensional array is an array of arrays. Define a two-dimensional array as follows:

java
public class Main {
  public static void main(String[] args) {
    int[][] ns = {
      { 1, 2, 3, 4 },
      { 5, 6, 7, 8 },
      { 9, 10, 11, 12 }
    };
    System.out.println(ns.length); // 3
  }
}

Because ns contains 3 arrays, therefore, ns.length is 3 . In fact, the structure of ns in memory is as follows:

                    ┌───┬───┬───┬───┐
         ┌───┐  ┌──▶│ 1 │ 2 │ 3 │ 4 │
ns ─────▶│░░░│──┘   └───┴───┴───┴───┘
         ├───┤      ┌───┬───┬───┬───┐
         │░░░│─────▶│ 5 │ 6 │ 7 │ 8 │
         ├───┤      └───┴───┴───┴───┘
         │░░░│──┐   ┌───┬───┬───┬───┐
         └───┘  └──▶│ 9 │10 │11 │12 │
                    └───┴───┴───┴───┘

If we define a normal array arr0 and assign ns[0] to it:

java
public class Main {
    public static void main(String[] args) {
        int[][] ns = {
            { 1, 2, 3, 4 },
            { 5, 6, 7, 8 },
            { 9, 10, 11, 12 }
        };
        int[] arr0 = ns[0];
        System.out.println(arr0.length); // 4
    }
}

In fact, arr0 obtains the 0th element of the ns array. Because each element of the ns array is also an array, the array pointed to arr0 is { 1, 2, 3, 4 } . In memory, the structure is as follows:

            arr0 ─────┐

                    ┌───┬───┬───┬───┐
         ┌───┐  ┌──▶│ 1 │ 2 │ 3 │ 4 │
ns ─────▶│░░░│──┘   └───┴───┴───┴───┘
         ├───┤      ┌───┬───┬───┬───┐
         │░░░│─────▶│ 5 │ 6 │ 7 │ 8 │
         ├───┤      └───┴───┴───┴───┘
         │░░░│──┐   ┌───┬───┬───┬───┐
         └───┘  └──▶│ 9 │10 │11 │12 │
                    └───┴───┴───┴───┘

To access an element of a two-dimensional array, you need to use array[row][col] , for example:

java
System.out.println(ns[1][2]); // 7

The length of each array element of a two-dimensional array does not need to be the same. For example, the ns array can be defined like this:

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

The structure of this two-dimensional array in memory is as follows:

                    ┌───┬───┬───┬───┐
         ┌───┐  ┌──▶│ 1 │ 2 │ 3 │ 4 │
ns ─────▶│░░░│──┘   └───┴───┴───┴───┘
         ├───┤      ┌───┬───┐
         │░░░│─────▶│ 5 │ 6 │
         ├───┤      └───┴───┘
         │░░░│──┐   ┌───┬───┬───┐
         └───┘  └──▶│ 7 │ 8 │ 9 │
                    └───┴───┴───┘

To print a two-dimensional array, you can use two levels of nested for loops:

java
for (int[] arr : ns) {
    for (int n : arr) {
        System.out.print(n);
        System.out.print(', ');
    }
    System.out.println();
}

Or use Arrays.deepToString() from the Java standard library:

java
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[][] ns = {
            { 1, 2, 3, 4 },
            { 5, 6 },
            { 7, 8, 9 }
        };
        System.out.println(Arrays.deepToString(ns));
    }
}

three-dimensional array

A three-dimensional array is an array of two-dimensional arrays. A three-dimensional array can be defined like this:

java
int[][][] ns = {
    {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    },
    {
        {10, 11},
        {12, 13}
    },
    {
        {14, 15, 16},
        {17, 18}
    }
};

Its structure in memory is as follows:

                              ┌───┬───┬───┐
                   ┌───┐  ┌──▶│ 1 │ 2 │ 3 │
               ┌──▶│░░░│──┘   └───┴───┴───┘
               │   ├───┤      ┌───┬───┬───┐
               │   │░░░│─────▶│ 4 │ 5 │ 6 │
               │   ├───┤      └───┴───┴───┘
               │   │░░░│──┐   ┌───┬───┬───┐
        ┌───┐  │   └───┘  └──▶│ 7 │ 8 │ 9 │
ns ────▶│░░░│──┘              └───┴───┴───┘
        ├───┤      ┌───┐      ┌───┬───┐
        │░░░│─────▶│░░░│─────▶│10 │11 │
        ├───┤      ├───┤      └───┴───┘
        │░░░│──┐   │░░░│──┐   ┌───┬───┐
        └───┘  │   └───┘  └──▶│12 │13 │
               │              └───┴───┘
               │   ┌───┐      ┌───┬───┬───┐
               └──▶│░░░│─────▶│14 │15 │16 │
                   ├───┤      └───┴───┴───┘
                   │░░░│──┐   ┌───┬───┐
                   └───┘  └──▶│17 │18 │
                              └───┴───┘

If we want to access an element of a three-dimensional array, for example, ns[2][0][1] , we only need to follow the positioning to find the corresponding final element 15 .

Theoretically, we can define any N-dimensional array. But in practical applications, except for two-dimensional arrays, which can be used sometimes, higher-dimensional arrays are rarely used.

Practise

You can use a two-dimensional array to represent the scores of a group of students in each subject. Please calculate the average score of all students:

java
public class Main {
    public static void main(String[] args) {
        // Student grades represented as two-dimensional arrays:
        int[][] scores = {
                { 82, 90, 91 }, 
                { 68, 72, 64 }, 
                { 95, 91, 89 }, 
                { 67, 52, 60 },
                { 79, 81, 85 },
        };
        // TODO:
        double average = 0;
        System.out.println(average);
        if (Math.abs(average - 77.733333) < 0.000001) {
            System.out.println("Test successful");
        } else {
            System.out.println("Test failed");
        }
    }
}

Summary

A two-dimensional array is an array of arrays, and a three-dimensional array is an array of two-dimensional arrays;

The length of each array element of a multidimensional array is not required to be the same;

You can use Arrays.deepToString() to print multi-dimensional arrays;

The most common multidimensional array is a two-dimensional array. To access an element of a two-dimensional array, use array[row][col] .

Multidimensional Array has loaded