Next: None. Up: Multidimensional arrays. Previous: None.


Multidimensional arrays

Textbook: Section 13.1

We should already understand how an array can be used to represent a sequence of data. Sometimes, however, you want to represent a table of data. And Java has a way of doing this: The two-dimensional array.

Declaring and creating 2-D arrays

In Java, you would create a two-dimensional array by adding an extra set of brackets.

int[][] mult;
This creates a name mult for a 2-D array.

Now, to allocate the space (that is, to create the array and assign mult to be a name for it, we can use the new keyword.

mult = new int[10][10];

Accessing 2-D array elements

To access an array element now, we'll have to use two indices: The first is the row index, and the second is the column index. In Java, you do this by putting the bracketed indices after each other.

mult[6][7] = 42;
This would set column 7 of row 6 in the table to hold the value 42.

Of course, we'll normally use loops to iterate through the table. Actually, we'd normally nest the loops.

for(int i = 0; i < 10; i++) {
    for(int j = 0; j < 10; j++) {
        mult[i][j] = i * j;
    }
}
This sets up a table as follows.
           0  0  0  0  0  0  0  0  0  0
           0  1  2  3  4  5  6  7  8  9
           0  2  4  6  8 10 12 14 16 18
           0  3  6  9 12 15 18 21 24 27
mult --->  0  4  8 12 16 20 24 28 32 36
           0  5 10 15 20 25 30 35 40 45
           0  6 12 18 24 30 36 42 48 54
           0  7 14 21 28 35 42 49 56 63
           0  8 16 24 32 40 48 56 64 72
           0  9 18 27 36 45 54 63 72 81

2-D arrays as arrays of arrays

Actually, Java can't distinguish between a two-dimensional array and an array of arrays. They're really the same thing - indeed, the only new syntax we've introduced through all of this is technique of creating a two-dimensional array. Internally, the computer will remember an array, where each array element points to a second array.

          +---+---+---+---+---+---+---+---+---+---+
mult ---> | + | + | + | + | + | + | + | + | + | + |
          +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
            |   |   |   |   |   :   :   :   :   :
            |   |   |   |   |
            +->0| 0 | 0 | 0 | 0   0   0   0   0   0
                |   |   |   |
                +->0| 1 | 2 | 3   4   5   6   7   8   9
                    |   |   |
                    +>0 | 2 | 4   6   8  10  12  14  16  18
                        |   |
                        +>0 | 3   6   9  12  15  18  21  24  27
                            |
                            +>0   4   8  12  16  20  24  28  32  36

We can use this fact if, for example, we only want to represent the lower half of the multiplication table in memory (and save all that duplicate upper half, a waste of memory). What we'll do is create each of the rows individually.

int[][] mult;
mult = new int[][10];
for(int i = 0; i < mult.length; i++) {
    mult[i] = new int[i + 1];
    for(int j = 0; j <= mult[i].length; j++) {
        mult[i][j] = i * j;
    }
}
Now we get something like the following instead.
          +---+---+---+---+---+---+---+---+---+---+
mult ---> | + | + | + | + | + | + | + | + | + | + |
          +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
            |   |   |   |   |   :   :   :   :   :
            |   |   |   |   |
            +->0|   |   |   |
                |   |   |   |
                +->0| 1 |   |
                    |   |   |
                    +>0 | 2 | 4
                        |   |
                        +>0 | 3   6   9
                            |
                            +>0   4   8  12  16

Quiz 1


Next: None. Up: Multidimensional arrays. Previous: Multidimensional arrays.