Multi-dimensional arrays |
Multi-dimensional arrays in Java are a simple extension of one-dimensional arrays, formed by just adding another subscript slot. For example if we were to rewrite the TicTacToe program in Java, we would declare the board as:
// The grid contents private int[][] marks = new int[NUM_ROWS][NUM_ROWS];
The type int[][] tell us that it is a two dimensional array holding ints. The construction expression new int[NUM_ROWS][NUM_ROWS]; actually creates the array with NUM_ROWS rows and columns.
The check to see if a particular row is a win for one of the players could be written:
for (int j = 0; j < NUM_COLS; j++) { if (marks[row][j] != matchMark) { return false; } }
Multi-dimensional arrays |