Java Notes

Arrays - 2-dimensional

1-, 2-, and higher-dimensional arrays

Java, as with most languages, supports multi-dimensional arrays - 1-dimensional, 2-dimensional, 3-dimensional, ... In practice most arrays are one-dimensional, and two-dimensional (rows and columns) are also quite common. Higher dimensional arrays are less common so they aren't used in the examples, but there's nothing mysterious about them and the same principles apply.

Two-dimensional arrays are used whenever the model data is best represented with rows and columns, or has two varying aspects (eg, gender and age, weight and height, ...). It's also the idea, altho not the implementation, of graphics that specifies a two (or three) dimensional position with an x and y (and z) coordinate.

Terminology. Other terms you will see for a two-dimensional array are matrix or table.

Visualizing two-dimensional arrays

2-dimensional arrays are usually represented with a row-column "spreadsheet" style. Assume we have an array, a, with two rows and four columns.

int[][] a = new int[2][3];  // Two rows and three columns.
a[0][0]a[0][1]a[0][2]a[0][3]
a[1][0]a[1][1]a[1][2]a[1][3]
Two-dimensional arrays are usually visualized as a matrix, with rows and columns. This diagram shows the array a with its corresponding subscripts.

Style: Use constants for the array sizes

It's very useful to define constants for the number of rows and columns.

static final int ROWS = 2;
static final int COLS = 3;
. . .
int[][] board = new int[ROWS][COLS];

Many row and column indexes (indices is the traditional English plural) have a meaning, and aren't just simply arbitrary numbers. The following example might be used to represent the number of accidents on by day of the month and the hour of the day. See programming problems below for some examples using this array.

static final int DAYS  = 31;
static final int HOURS = 24;
. . .
int[][] accidents = new int[DAYS][HOURS];

Initial values

You can assign initial values to an array when in a manner very similar to one-dimensional arrays, but with an extra level of braces. The dimension sizes are computed by the compiler from the number of values. This would allocated a 3x3 board

int[][] board = new int[][] {{1,0,0},{0,1,0},{1,2,1}};

Use nested for loops to process 2-dimensional arrays

Two-dimensional arrays are almost always processed with nested for loops.

static final int ROWS = 2;
static final int COLS = 4;
. . .
int[][] a2 = new int[ROWS][COLS];
. . .
//... Print array in rectangular form
for (int i =0; i < ROWS; i++) {
    for (int j = 0; j < COLS; j++) {
        System.out.print(" " + a2[i][j]);
    }
    System.out.println("");
}

Programming problems

  1. For the accidents array above write code that does the following:
    1. Display the total number of accidents? Hint: Use nested loops to add all elements.
    2. At what hour do the most accidents occur? Hint: Use on outer loop over hours, where the inner loop goes down the column and adds all the accidents for that hour. Keep max as you would for a one-dimensional array.
    3. How would you declare a three-dimensional array that had an additional dimension for the 12 months?

More

See Two-dimensional arrays as "arrays of arrays".