PROGRAMMING FUNDAMENTALS › JAVA FUNDAMENTALS
The type int[] describes one array level: an array whose elements are int values. The type int[][] describes two array levels: an array whose elements are int[] values. In quizScores, the outer array is the first level, and each row is a second-level int[] array. The phrase two-dimensional describes these two levels of arrays, not a promise that every row has the same length.
int[][] quizScores = {{8, 6, 9}, {7, 5}, {10, 8, 6, 9}};
int[] firstRow = {8, 6, 9};The braces contain three rows, but the number of values in a row does not change the declared type. A row with two values is still an int[], just like a row with four values is still an int[]. The outer value that contains those rows is therefore an int[][].
Which description matches quizScores?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Java does not need to store quizScores as one flat grid. It stores an outer array with three elements. Each outer element is a reference to a separate int[] row: outer index 0 refers to [8, 6, 9], index 1 refers to [7, 5], and index 2 refers to [10, 8, 6, 9]. The rows are separate array values connected through the outer array.
int[][] quizScores = {{8, 6, 9}, {7, 5}, {10, 8, 6, 9}};
// Conceptually:
// quizScores -> [row reference, row reference, row reference]
// index 0 index 1 index 2
// [8, 6, 9] [7, 5] [10, 8, 6, 9]This structure explains why an int[][] can have rows of different sizes. The outer array only stores references. Each referenced row is created with its own number of int elements, so the row at index 1 does not need to match the row at index 0 or index 2.
quizScores.length describes the outer array, so its value is 3 because there are three rows. It does not describe the number of integers in every row. After selecting a row, quizScores[r].length describes that selected row. The three row lengths are quizScores[0].length equal to 3, quizScores[1].length equal to 2, and quizScores[2].length equal to 4.
int outerSize = quizScores.length; // 3
int rowZeroSize = quizScores[0].length; // 3
int rowOneSize = quizScores[1].length; // 2
int rowTwoSize = quizScores[2].length; // 4A row of length 2 has valid indices 0 and 1. Therefore quizScores[1][2] is invalid: the first index selects row 1, and that row has no element at column index 2. The valid column range depends on the row you selected, so one fixed column limit cannot describe all three rows.
Replace the invalid access quizScores[1][2] with the direct access that selects 5.
quizScores[1][2]Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
An index operation selects one element from one array level. Starting with quizScores, the value has type int[][]. Applying [1] selects the row at outer index 1, so quizScores[1] has type int[]. Applying another [0] selects the first integer in that row, so quizScores[1][0] has type int and value 7.
int[][] allScores = quizScores; // type int[][]
int[] selectedRow = quizScores[1]; // type int[], value [7, 5]
int selectedScore = quizScores[1][0]; // type int, value 7
int lastScoreInRowTwo = quizScores[2][3]; // type int, value 9The two indices in quizScores[2][3] perform two separate selections. First, index 2 selects the row [10, 8, 6, 9], whose type is int[]. Then, index 3 selects its fourth element, which is the int value 9. The first index must be valid for the outer array, and the second must be valid for the selected row.