Skip to main content
ICT
Lesson AB23 - Two-Dimensional Arrays
 
Main Previous Next
Title Page >  
Summary >  
Lesson A1 >  
Lesson A2 >  
Lesson A3 >  
Lesson A4 >  
Lesson A5 >  
Lesson A6 >  
Lesson A7 >  
Lesson A8 >  
Lesson A9 >  
Lesson A10 >  
Lesson A11 >  
Lesson A12 >  
Lesson A13 >  
Lesson A14 >  
Lesson A15 >  
Lesson A16 >  
Lesson A17 >  
Lesson A18 >  
Lesson A19 >  
Lesson A20 >  
Lesson A21 >  
Lesson A22 >  
Lesson AB23 >  
Lesson AB24 >  
Lesson AB25 >  
Lesson AB26 >  
Lesson AB27 >  
Lesson AB28 >  
Lesson AB29 >  
Lesson AB30 >  
Lesson AB31 >  
Lesson AB32 >  
Lesson AB33 >  
Vocabulary >  
 

A. Two-Dimensional Arrays page 3 of 9

  1. Often the data a program uses comes from a two-dimensional situation. For example, maps are two-dimensional (or more), the layout of a printed page is two-dimensional, a computer-generated image (such as on your computer's screen) is two-dimensional, and so on.

    For these situations, a Java programmer can use a two-dimensional array. This allows for the creation of table-like data structures with a row and column format. The first subscript is a row index in a table while the second subscript is a column index in a table. Here is example code, in Sample Code 23-1, including a diagram of the array table.

Sample Code 23-1

int[][] table = new int[3][4];
int row, col;

for (row = 0; row < 3; row++){
   for (col = 0; col < 4; col++){
      table[row][col] = row + col;
   }
}

  1. Two-dimensional arrays are objects. A variable such as table is a reference to a 2D array object. The declaration

    int[][] table;

    defines a table that holds a reference to a 2D array of integers. Without any further initialization, table holds the value null.

  2. The declaration

    int[][]table = new int[3][4];

    defines a table that holds a reference to a 2D array of integers, creates an array object of 3 rows and 4 columns, and puts the reference in table. All the elements of the array are initialized to zero.

  3. The declaration

    int[][] table = { {0,0,0,0},
                      {0,0,0,0},
                      {0,0,0,0} };

    does exactly the same thing as the previous declaration (and would not ordinarily be used.)

  4. The declaration

    int[][]table = { {0,1,2,3},
                     {1,2,3,4},
                     {2,3,4,5} };

    creates an array of the same dimensions (same number of rows and columns) as the previous array and initializes the elements to the given values in each cell.

  5. If no initializer is provided for an array, then when the array is created, it is automatically filled with the appropriate values: zero for numbers, false for boolean, and null for objects.

  6. Just as with one-dimensional arrays, the row and column numbering of a 2-D array begins at subscript zero (0). The 3 rows of the table are numbered from 0...2. Likewise, the 4 columns of the table are numbered from 0...3.

  7. The array table is filled with the sums of row and col, which is accomplished by Sample Code 23-2 (see below). To access each location of the matrix, specify the row coordinate first, then the column:

    table[row][col]

    Each subscript must have its own square brackets.

  8. The length of a 2D array is the number of rows it has. The row index will run from 0 to length -1. The number of rows in table is given by the value table.length.

    Each row of a 2D array has its own length. To get the number of columns in table, use any of the following:

    table[0].length
    table[1].length
    table[2].length.

    There is actually no rule that says that all the rows of an array must have the same length, and some advanced applications of arrays use varying-sized rows. But if you use the new operator to create an array in the manner described above, you'll always get an array with equal-sized rows.

  9. The routine that assigned values to the array used the specific numbers of rows and columns. That is fine for this particular program, but a better definition would work for an array of any two dimensions.

    Sample Code 23-2

    int[][] table = new int[3][4];
    int row, col;

    for (row = 0; row < table.length; row++){
       for (col = 0; col < table[row].length; col++){
          table[row][col] = row + col;
       }
    }

    In Sample Code 23-2, the limits of the for loops have been redefined using table.length and table[row].length so that they work with any two-dimensional array of ints with any number of rows and columns .

 

Main Previous Next
Contact
 © ICT 2006, All Rights Reserved.