8. Arrays
Programming Project 2021/22

8.2. Multidimensional Arrays

A multidimensional array can be simply defined as an array of arrays.

We declare such an array by using two or more square brackets:

String[][] names;
int[][][] cube;

We add one pair of square brackets per dimension we want in our array.

A two-dimensional array resembles a table, while a three-dimensional array, a cube.

Declaring and constructing a multidimensional array:

String[][] names = new String[2][2];
int[][][] cube = new int[3][3][3];

Declaring, constructing, and initializing a multidimensional array:

String[][] names = {{"John", "Jane", "Jamie"}, {"Jack", "Mary", "Cate"}};
int[][][] cube = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}};

Each element must be accessed by a corresponding number of index values.

names[0][1]
cube[0][0][0]

Source

Multidimensional arrays in memory

This is what our multidimensional arrays look like in memory.

array java memory

Source

About multidimensional arrays

Multidimensional arrays may have sub-arrays of different sizes.

int[][] array2 = {{1, 2}, {3}, {4, 5, 6}};

In two-dimensional arrays (aka matrices), by convention, we assume that

  • the first dimension represents the rows and
  • the second dimension represents the columns.

Iterating over elements of a multidimensional array

Here is an example on how to iterate over all the elements of a multidimensional array.

public class TwoDimensionalArrays {
  public static void main(String[] args) {
    int[][] array1 = {{1, 2, 3}, {4, 5, 6}};
    int[][] array2 = {{1, 2}, {3}, {4, 5, 6}};

    System.out.println("\nValues in array1 by row are: ");
    printTwoDimensionalArray(array1);

    System.out.println("\nValues in array2 by row are: ");
    printTwoDimensionalArray(array2);
  }

  public static void printTwoDimensionalArray(int[][] array) {
    for (int row = 0; row < array.length; row++) {
      System.out.print("[");

      for (int column = 0; column < array[row].length; column++) {
        System.out.print(array[row][column]);

        if (column < array[row].length - 1) {
          System.out.print(", ");
        }
      }

      System.out.println("]");
    }
  }
}

Exercise 3

Lets us revisit exercise 1 with multidimensional arrays.

Write a second sum method that sums all the numbers in a three-dimensional array.

public class SummingArrays {
  public static int sum(int[][][] cube) {
    // FIX ME
    return -1;
  }

  public static void main(String[] args) {
    int[][][] cube1 = {
            {{1, 1}, {1, 1}, {1, 1}},
            {{1, 1}, {1, 1}, {1, 1}},
            {{1, 1}, {1, 1}, {1, 1}}
    };

    System.out.println("Should return 18 for cube1 = " + sum(cube1));

    int[][][] cube2 = {
            {{1, 0}, {1, 0}, {1, 0}},
            {{1, 0}, {1, 0}, {1, 0}},
            {{1, 0}, {1, 0}, {1, 0}},
    };

    System.out.println("Should return 9 for cube2 = " + sum(cube2));
  }
}

You can find the solution to this exercise here.