Sub topic 6
Multidimensional and Jagged Arrays
A multidimensional array is an array whose elements are themselves arrays. In Java, a two-dimensional array is often used to represent rows of values, like a small table.
Each row is a separate array. This means the rows do not have to be the same length.
Creating rows
The type int[][] describes an array of int[] arrays. For example, this initializer creates three rows:
int[][] rows = {{1, 2}, {3}, {}};
An initializer supplies the starting values when the array is created. Here, the first row has two values, the second has one, and the third is empty.
An array with rows of different lengths is called a jagged array.
You can also create rows of the same length with new. For example, new int[2][3] creates two rows, each with three int elements. Their starting values are all zero.
Accessing an element
Use two indexes to reach a value. An index is a position number, starting at zero.
In rows[0][1], the first index selects row 0. The second selects position 1 in that row. The value is 2. You can change it with rows[0][1] = 5.
The outer array and each row have their own lengths:
rows.lengthis the number of rows.rows[0].lengthis the number of elements in row 0.rows[2].lengthis0, because that row is empty.
An index must be at least zero and less than the length of the array it selects from. For example, rows[1][1] is invalid: row 1 has only one element, at index 0.
Walk through every row
A nested loop is a loop placed inside another loop. The outer loop can visit each row, while the inner loop visits each value in that row.
Save this program as MultiArrays.java, then compile and run it with javac MultiArrays.java and java MultiArrays.
public class MultiArrays {
public static void main(String[] args) {
int[][] rows = {{1, 2}, {3}, {}};
rows[0][1] = 5;
for (int r = 0; r < rows.length; r++) {
System.out.print("Row " + r + ":");
for (int c = 0; c < rows[r].length; c++) {
System.out.print(" " + rows[r][c]);
}
System.out.println();
}
rows[1] = rows[0];
rows[1][0] = 9;
System.out.println("Shared value: " + rows[0][0]);
}
}The output is:
Row 0: 1 5
Row 1: 3
Row 2:
Shared value: 9The inner loop uses rows[r].length, not the length of a different row. For the empty row, that length is zero, so the inner loop runs zero times.
Rows can be shared or missing
A reference is a value that points to an object. Each element of the outer array holds a reference to a row array.
In the program, rows[1] = rows[0] makes both positions refer to the same row. Changing that row through rows[1] also affects what you see through rows[0].
A row reference can also be null, meaning it points to no row. If a row might be null, check it before using its length or accessing an element. An empty row is different: it is a real array with length zero.
Exercise
For int[][] data = {{4}, {5, 6}}, what are data.length and data[1].length?
Show answer
Both are 2. There are two rows, and row 1 has two elements.
Exercise
If data[0] = data[1] runs, what happens when you change data[0][0]?
Show answer
The same change is visible through data[1][0] because both positions refer to the same row.