1. 2D arrays (Tables)
2. Nested Loops
2D arrays (Tables)
2D arrays are just an extension of 1D arrays -- a 2D array is simply a 1D array
whose elements are themselves 1D arrays:
int value; // plain integer variable
int[] numbers; // a list/array whose elements are integers
int[][] table; // a list/array whose elements are lists/arrays
Realizing that a 2D array is just a hierarchy of 1D arrays helps in understanding
how to process 2D arrays -- in fact, there is little new compared to 1D arrays.
2D Arrays
Creating
--------
int[][] table = { {1, 2, 3, 4}, // creates a 2D array/table
{5, 6, 7, 8}, // of 3 rows, 4 columns
{9, 10, 11, 12} };
Visual representation
---------------------
table
| columns
|0 1 2 3
+---------------+
| 1 | 2 | 3 | 4 | row 0
+---------------+
| 5 | 6 | 7 | 8 | row 1
+---------------+
| 9 | 10| 11| 12| row 2
+---------------+
table (more accurate representation -- shows that a 2D array
| is just a 1D array whose elements are 1D arrays)
| columns
| 0 1 2 3
+---+ ---------------
| *-|-->| 1 | 2 | 3 | 4 | row 0
| | ---------------
+---+ ---------------
| *-|-->| 5 | 6 | 7 | 8 | row 1
| | ---------------
+---+ ---------------
| *-|-->| 9 | 10| 11| 12| row 2
| | ---------------
+---+
Getting the length
------------------
table.length -- will tell us how many rows the table has, i.e.
how many elements (1D arrays) the main 1D array has
table[0].length -- will tell us how many cells (columns) the first row has
table[2].length -- will tell us how many cells (columns) the last row has
Accessing individual elements
-----------------------------
table[0] -- provides access to the individual rows in the table
table[1]
table[2]
Since the individual rows are just 1D array
we can use [] to access individual cells:
table[0][0] = 100; // set the cells of the first row (row 0)
table[0][1] = 101; // to the values 100, 101, 102, 103
table[0][2] = 102;
table[0][3] = 103;
int i = table[0][0]; // read the contents of the cells on first row (row 0)
int j = table[0][1]; // and store in the variables i, j, k, l
int k = table[0][2];
int l = table[0][3];
When the size of the 2D array is known in advance we can create a table of the
given number of elements:
Creating when dimensions known
------------------------------
int[][] myTable = new int[3][4]; // myTable has 3 rows of 4 columns of
// empty cells (initialized to 0)
Ragged/Jagged Arrays
Keep in mind that the rows of a 2D array do not have to have the same size.
2D arrays whose rows have different sizes are called ragged/jagged arrays.
In Java the rows do not have to have the same size/length:
int[][] raggedArray = { {1, 2, 3, 4},
{5, 6},
{7, 8, 9},
{10} };
In this case row 0 has 4 elements -- 1 2 3 4
row 1 has 2 elements -- 5 6
row 2 has 3 elements -- 7 8 9
row 3 has 1 element -- 10
Example -- displaying a 2D array
The first useful method for 2D arrays is a method that can print the 2D array
nicely formatted on the screen:
Example: Write a method printTable which takes a 2D array of integers as
parameter and displays the contents of the 2D array with the rows on separate
lines and the cells separated by spaces.
Outline:
1. For each row index in the table:
2. display the current row
In Step 2 we have to display the current row, which is just a 1D array.
Fortunately, we already have a method to do just that (the method
printArray -- the first method we, wrote for 1D arrays).
Here is the final code:
// displays a 1D array with all elements separated by spaces
void printArray( double[] array )
{
for ( int i = 0 ; i < array.length ; i = i + 1)
{
System.out.print( array[i] + " " );
}
System.out.println();
}
// displays a 2D array -- each row on separate line
// cells separated by spaces
void printTable( double[][] table )
{
for ( int r = 0 ; r < table.length ; r = r + 1 ) // Step 1.
{
printArray( table[r] ); // Step 2.
}
}
Nested loops
In the previous example, printTable the processing of a 2D array was achieved
via a loop that traversed the individual rows of the table and dispatched the
processing of the current row to the helper method printArray.
When processing 2D arrays it is common (and often convenient) to describe the
computation via nested loops.
Compare the two possible ways to express how to display a 2D array:
Original outline:
1. For each row index in the table:
2. display the current row
New outline:
1. For each row index in the table:
2. For each column index in the current row:
3. display the current cell
Both descriptions specify how to carry out the process of displaying a 2D array.
The second outline is more explicit in its description.
The main difference between the two is in Step 2: the original method hides
the fact that another loop is used to traverse the elements of the current row;
this is made explicit in the second version.
In the code below notice the following:
- c -- the new index variable used to traverse the current row
- table[r].length -- retrieves the length of the current row
- table[r][c] -- retrieves the value of the element at row r and column c
Here is the code for the new version:
// displays a 2D array -- each row on separate line
// cells separated by spaces
void printTable( double[][] table )
{
for ( int r = 0 ; r < table.length ; r = r + 1) // Step 1
{
for ( int c = 0 ; c < table[r].length ; c = c + 1 ) // Step 2
{
System.out.print( table[r][c] + " " ); // Step 3
}
System.out.println();
}
}