Two Dimensional Arrays in Java
A table organized in rows and columns is a very effective means for communicating many different types of information. E.g., to show high temperatures versus a date for different locations:
|
10/2 |
10/3 |
10/4 |
10/5 |
10/6 |
|
53 |
61 |
65 |
45 |
30 |
|
42 |
56 |
63 |
68 |
46 |
|
60 |
63 |
42 |
30 |
25 |
Or distance table between cities in miles:
|
LA |
SF |
|
|
|
LA |
|
600 |
500 |
150 |
450 |
SF |
600 |
|
100 |
750 |
150 |
|
500 |
100 |
|
650 |
50 |
|
150 |
750 |
650 |
|
600 |
|
450 |
150 |
50 |
600 |
|
Or, a tuition table:
|
Day |
Boarding |
Grades 1-6 |
6,000 |
18,000 |
Grades 7-8 |
9,000 |
21,000 |
Grades 9-12 |
12,000 |
24,500 |
This is referred to as tabular format. In Java, we represent these as two dimensional arrays.
In Java, one declares a two dimensional array the same as in C or C++, but the memory allocation by the compiler is different. E.g., in C one declares a two-dimensional array of int as
int [ ][ ] table = new int[3][5];
The memory management system will allocate 60 (3*5*4) bytes of contiguous memory. It will store the two dimensional array as row major. This means it first stores the first row of the table, then the second row, etc: (table[0][0],table[0][1],table[0][2],table[0][3],table[0][4],table[1][0],table[1][1]…)
where table[i][j] means the element in the ith row and jth column.
The address of table[i][j] then is calculated as
address of table + i*4*5 + j*4
The 4*5 is because each row has 5 elements, each of 4 bytes. So if the array is given having the following data:
2 |
6 |
-8 |
9 |
10 |
5 |
8 |
9 |
6 |
-8 |
4 |
3 |
5 |
7 |
8 |
In memory, it will look like, assuming the array starts at address 200:
200 204 208 212 216 220 224 228 232 236 240 244 248 252 256
2 |
6 |
-8 |
9 |
10 |
5 |
8 |
9 |
6 |
-8 |
4 |
3 |
5 |
7 |
8 |
So table[2][3] is at address 200+2*4*5+4*3 = 252.
However, in Java, one allocates a table of addresses, just as for an array of objects. I.e.,
int[ ][ ] table = new int[3][5];
in Java means
int[ ][ ] table;
table = new int[3][ ];
table[0] = new int[5];
table[1] = new int[5];
table[2] = new int[5];
which is equivalent to:
int[][] table;
table = new int[3][];
for(int i = 0; i < 3; i++)
table[i] = new int[5];
That is to say, table is a one-dimensional array, but each of its values is an address where a one dimensional array of size 5 can be found. In Java, we only have one dimensional array as a data structure. If we want to represent higher dimensional arrays, we must have one dimensional array, each element inside the array is pointing to another array.
Symbolically, it looks like:
|
|
|
The table on the left is table, while the three other tables are table [0], table [1], and table[2]. Each of the 15 boxes on the right represents a storage place for an int. Each of the 3 boxes on the right represents the storage place for an address.
We can also declare a two-dimensional array and initialize it as we do one dimensional arrays. Remember, that we can create a one-dimensional array in Java as
int [ ] list = {1,2,3,3,5};
which creates a one dimensional array of size 5, with list[0] = 1, list[1] = 2, list[3] = 3, and list[4] = 5. We can do the same with two dimensional arrays as
int [ ] [ ] list ={ {2,6,-8,9,10},{5,8,9,6,-8},{4,3,5,7,8}};
which creates a two dimensional array of size [3][5].
Note that one can have irregular dimensional two dimensional arrays in Java. That is to say, the following can be done in Java:
int [ ][ ] table;
table = new int[3][ ];
table[0] = new int[4];
table[1] = new int[3];
table[2] = new int[5];
We call such an array a ragged array.
Thus, row 0 has 4 elements, row 1 has 3, and row 2 has 5 elements.
Suppose we declare a two dimensional array
int [ ] [ ] table = new int [3][5];
Then what is table.length equal to? Remember that in Java all arrays are one-dimensional. To declare a two-dimensional array really means to declare a one-dimensional array each of whose elements is a one-dimensional array. Hence, there is no ambiguity of table.length. It is 3, as table is a one-dimensional array of 3 elements, each of whose elements happens to point to a 5 dimensional array. We refer to the dimensions of each of the elements of table by table[i].length, for i = 0, 1, or 2. Each of them is of length 5 in this case.
Notice in our ragged array above, table[0].length = 4, table[1].length =3, and table[2].length = 5.