We have looked at arrays in one dimension. As we have seen, these can be used for lots of purposes.
Sometimes, however, we want to use a multi-dimensional array which is an array that has more than one index.
The simplest type of multi-dimensional array is a two dimensional one. In declaring a 2D array, we need to give two sizes, one for the number of rows, and one for the number of columns. For example:
int array[][] = new int[3][5];
This would create a two-dimensional grid that would look something like this:
When using the array, we specify both the row and column to be used:
array[0][2] = 9;
array[2][4] = 18;
Running this code would give us:
9 | ||||
18 |
You can also think of a multi-dimensional array as an "array of arrays".
Multi-dimensional arrays allow us to view our data in an intuitive way, but memory is really one dimensional. The 2D array:
1 | 2 | 3 | 4 | 5 |
6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 |
Would actually be stored in memory as follows:
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
In order to convert a two-dimensional address such as:
array[row][column]
Into a one-dimensional address, the compiler uses this formula:
array[row*COLUMNS + column]
Where "COLUMNS" is the number of columns in the array.
The example below stores a multiplication table in a 2D array. It also demonstrates passing 2d arrays to and from functions:
class TimesTable {
// function which creates a 2d times table of a given size
public static int[][] createTimesTable(int size) {
int times_table[][];
times_table = new int[10][10];
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
times_table[i][j] = (i + 1) * (j + 1);
}
}
return times_table;
}
// function which prints out a 2d table of integers
public static void printTimesTable(int table[][]) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
System.out.print(table[i][j] + " ");
}
System.out.println();
}
}
public static void main(String args[]) {
// build the times table
int times_table[][] = createTimesTable(10);
// print it out
printTimesTable(times_table);
}
}
This example also demonstrates nested for loops which is a very common pattern when using multi-dimensional arrays.
We can also use 2d arrays to represent mazes. Here, the start of the maze is the upper-left corner, and the finish is the bottom right of the maze.
class Maze {
// function which returns a (hard-coded) 2d maze
public static char[][] getMaze() {
return new char [] []
{
{'-','#','#','#','#','-','-','-','-','-','-','#','#','#','#','#','#','#'},
{'-','-','-','-','#','-','#','#','#','#','-','-','-','-','-','-','#','#'},
{'-','#','#','-','#','-','-','-','#','#','#','#','#','#','#','-','#','#'},
{'-','#','#','#','#','-','#','-','#','#','#','-','-','-','#','-','-','-'},
{'-','-','-','-','-','-','#','-','#','#','#','-','#','#','#','-','#','#'},
{'#','#','-','#','#','#','#','-','#','#','#','-','#','#','#','-','#','#'},
{'#','#','#','#','#','#','#','-','-','-','-','-','-','-','#','-','-','-'},
{'#','#','-','-','-','-','-','-','#','#','#','#','#','#','#','#','#','#'},
{'-','-','-','#','#','#','#','#','#','#','-','-','-','-','#','#','#','#'},
{'-','#','-','-','-','-','-','-','-','-','-','#','#','-','-','-','-','#'},
{'-','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
{'-','-','-','-','-','-','-','-','-','-','-','-','#','#','#','#','#','#'},
{'#','#','#','#','#','#','#','#','#','#','#','-','-','-','-','-','-','-'},
};
}
// function which prints out a 2d maze
public static void printMaze(char maze[][]) {
for (int i = 0; i < maze.length; i++) {
for (int j = 0; j < maze[0].length; j++) {
System.out.print(maze[i][j]);
}
System.out.print('\n');
}
}
public static void main(String args[]) {
char maze[][] = getMaze();
printMaze(maze);
}
}
Any kind of grid-like information can be represented with 2d arrays.
How can we develop an algorithm to solve a maze stored in a 2d array like this?
We can also have arrays that have even more dimensions. A three-dimensional array can be declared as:
int array[][][] = new int[10][10][10];
This will create a three dimensional array containing 1,000 integers.
Arrays of more than two dimensions are not very commonly done, but there is no limit to the number of dimensions you could create.
Copyright © 2024 Ian Finlayson | Licensed under a Creative Commons BY-NC-SA 4.0 License.