knowledge-kitchen

Multi-Dimensional Arrays (in Java)

The universe is not a stagnant place where technology stands still and only the few govern its destiny. Rather, it is a multidimensional dynamic entity that interacts with all things, even the very smallest. And what part we each place in it and the effect we have on it is a matter of our own choice.

-R.G. Risch, Beyond Mars: Crimson Fleet

  1. Overview
  2. Truth
  3. Ragged Arrays
  4. Rows & Columns
  5. Conclusions

Overview

Concept

Multidimensional arrays are arrays that store other arrays nested within them.

Two-dimensional example

A two-dimensional array - an array that stores inner arrays of type int.

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

Two-dimensional example (continued)

To output the third value in the second inner array - 3

System.out.println( numbers[1][2] );

The syntax to modify one of the values in a two dimensional array is the same.

// modify the third value in the second inner array
numbers[1][2] = 11;

The entire inner array can also be replaced with a different one.

int[] newInnerArray = { 13, 12, 11 };
numbers[1] = newInnerArray;

Three-dimensional example

A three-dimensional array - an array that stores inner arrays of type String.

String[][][] words = {
        {
            {"who", "what", "when", "why", "how"},
            {"whither", "whence", "wherefore"}
        },
        {
            {"there", "that", "then"},
            {"thence", "hence", "heretofore"}
        }
};

To output the third value in the second child array of the first inner array - ‘wherefore’.

System.out.println( words[0][1][2] );

Truth

It’s all a lie!

Multidimensional arrays do not exist in Java.

It’s all a lie! (continued)

the truth

Ragged Arrays

Concept

Inner arrays may have different lengths… such arrays are called ragged arrays.

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

Without syntactic sugar

When not using array syntactic sugar, it is possible to leave the length of all but the first dimension blank in the allocation statement to allow for ragged arrays.

int[][] numbers = new int[3][]; // blank second dimension

// now populate each inner array with whatever length you prefer
int[] newInnerArray1 = { 10, 9, 8, 7, 6}
int[] newInnerArray2 = { 1, 2 }
int[] newInnerArray3 = { 6, 5, 4, 4, 3, 2, 1 }
numbers[0] = newInnerArray1;
numbers[1] = newInnerArray2;
numbers[2] = newInnerArray3;

Rows & Columns

Concept

It is best to think of two-dimensional arrays as rows and columns of data, like a spreadsheet or table.

String[][] naughtsAndCrosses = {
    { "X", "O", "X" }, // first row
    { "X", "O", "X" }, // second row
    { "X", "O", "X" }  // third row
};

Looping through rows with syntactic sugar

To loop through each row, remember that each row of a two-dimensional array is itself a one-dimensional array.

Using for loop syntactic sugar.

// iterate through each row in the outer array
for (String[] row : naughtsAndCrosses ) {

    // print out the row
    System.out.println( Arrays.toString(row) );

}

Looping through rows with a counter

If you insist on using an accumulator-style for loop, that also works, of course.

Using for loop accumulator syntax.

// iterate through each index value in the outer array
for ( int i=0; i<naughtsAndCrosses.length; i++ ) {

    // get the row at this index
    String[] row = naughtsAndCrosses[i];

    // print out the row
    System.out.println( Arrays.toString(row) );

}

Looping through each column within each row

It’s easy enough to iterate through each column within each row.

Using for loop syntactic sugar syntax.

// iterate through each row in the outer array
for (String[] row : naughtsAndCrosses ) {

    // iterate through each column within the current wor
    for (String col : row) {

        // print out the column value
        System.out.print( col );

    }

}

Looping through each column within each row (the hard way)

The same can be done without syntactic sugar

Using for loop accumulator syntax.

// iterate through each index value in the outer array
for ( int i=0; i<naughtsAndCrosses.length; i++ ) {

    // get the row at this index
    String[] row = naughtsAndCrosses[i];

    // iterate through each index value of the inner array
    for (int j=0; j<row.length; j++) {

        // print out the column value
        System.out.println( row[j] );

    }

}

Naughts and Crosses example

A fuller-featured example of using a two-dimensional array to represent the board in a game of tic tac toe is available here.

Conclusions

You now have a basic understanding of multidimensional arrays - especially two-dimensional arrays - in Java.