Difficulty: Beginner to Intermediate | Prerequisites: Java Identifiers and Data Types notes, Functions notes.
Big picture
Arrays let you store multiple values of the same type under a single variable name, accessed by index. Once you move beyond single variables, arrays are how you handle lists, tables and collections of data. The 2D array extends this to rows and columns, which is how you represent grids, matrices and tabular data. The exam tests zero-based indexing, the relationship between array size and last valid index, the single-type constraint, 2D indexing (row first, column second) and iterating with nested loops.
Arrays hold multiple values of one type. Indexing starts at 0, so an array of size 5 has valid indices 0 through 4. All elements must be the same type. A 2D array uses two indices: the first for the row, the second for the column. To process every element, use nested for-loops (outer loop for rows, inner loop for columns).
Array
A data structure that stores a fixed number of values of the same type, accessed by a numeric index. Arrays are reference types in Java, not primitives. Think of it as a row of numbered boxes, all holding the same kind of thing.
Zero-based indexing
Java arrays start counting at 0. The first element is at index 0, the second at index 1, and so on. An array of size n has valid indices 0 through n-1.
Array .length
A property (not a method) that returns the number of elements in the array. For a 1D array nums of size 5, nums.length returns 5, but the last valid index is 4.
2D array
An array of arrays, used to represent data in rows and columns. Declared as int[][] data = new int[3][3]; for a 3-row, 3-column grid.
Row index (first bracket)
In a 2D array access like data[1][2], the first bracket (1) specifies the row.
Column index (second bracket)
In data[1][2], the second bracket (2) specifies the column.
Declaring and using 1D arrays
Declaration: int[] nums = new int[5]; creates an array of 5 integers.
Initialisation with values: int[] nums = {10, 20, 30, 40, 50};
Access: nums[0] is 10, nums[4] is 50.
All elements must be the same type. You cannot mix int and String in a single array, even though both are valid types on their own.
The .length property gives you the size: nums.length returns 5.
Zero-based indexing and the off-by-one trap
An array of size 5 has indices 0, 1, 2, 3, 4.
The last element is always at index array.length - 1.
Accessing nums[5] on a size-5 array causes an ArrayIndexOutOfBoundsException.
2D arrays: rows and columns
Consider this 2D array:
int[][] data = {
{15, 25, 35}, // Row 0
{45, 55, 65}, // Row 1
{75, 85, 95} // Row 2
};data[1][1] retrieves 55. The first index (1) is the row, the second index (1) is the column.
data[0][2] retrieves 35 (row 0, column 2).
data[2][0] retrieves 75 (row 2, column 0).
data.length gives the number of rows (3).
data[0].length gives the number of columns in row 0 (3).
Iterating through a 2D array
To process every element (for example, to sum all values), use nested for-loops:
int sum = 0;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
sum += matrix[i][j];
}
}The outer loop iterates through rows (i < matrix.length).
The inner loop iterates through columns in each row (j < matrix[i].length).
sum += matrix[i][j] adds each element to the running total.
Students think the last index of a size-5 array is 5. It is 4. Indexing starts at 0, so the valid range is 0 to length - 1.
Students assume arrays can hold mixed types as long as they are all primitives. They cannot. An array holds one type only, the type you declared it with.
Students think the first bracket in a 2D array refers to the column. It refers to the row. data[row][col].
Students forget that .length on a 2D array gives the number of rows, while .length on a specific row gives the number of columns in that row.
Last index of an array (Q20): size 5 means last index is 4. A near-guaranteed exam question.
Single-type constraint (Q25): arrays cannot hold mixed types.
2D indexing order (Q26, Q41): first bracket is row, second is column. Know this cold.
Iterating a 2D array (Q54): you will be asked to write or explain nested for-loops that sum, print or process all elements.
The data matrix example (Q41): be able to retrieve any value by row and column index.
An array arr has size 7. What is the index of its last element? Answer: 6.
True or false: An array can hold both int and double values. Answer: False. All elements must be the same type.
In data[2][0], which number is the row and which is the column? Answer: 2 is the row, 0 is the column.
What does data.length return for a 2D array with 4 rows and 3 columns? Answer: 4 (the number of rows).
True or false: The first bracket in a 2D array refers to the column. Answer: False. It refers to the row.
Q: If you have an array nums of size 5, what index would you use to access the last element?
A: Index 4. Array indexing starts at 0, so the indices are 0, 1, 2, 3, 4.
Q: Write the Java statement to retrieve the value 55 from the following 2D array, and explain which index is the row and which is the column.
Row 0: 15, 25, 35 | Row 1: 45, 55, 65 | Row 2: 75, 85, 95
A: data[1][1]. The first index (1) is the row (Row 1), and the second index (1) is the column (second column). 55 sits at row 1, column 1.
Q: Describe the process and code for iterating through every element of a 2D array named matrix to find the sum of all elements.
A: Use nested for-loops. The outer loop iterates through each row (i < matrix.length), and the inner loop iterates through each column in that row (j < matrix[i].length). Inside the inner loop, add each element to a running total: sum += matrix[i][j].
Q: True or false: The values in an array can be of different data types as long as they are all primitives.
A: False. An array can only hold the type it was declared with. You cannot mix types in a single array.
Arrays connect to String methods because both use zero-based indexing and the concept of "up to but not including" an end position (as in .substring(start, end)). Arrays of objects bring together this chapter and OOP (Chapter 7): you can create an array of Student objects, for example, and iterate through it with a loop. Passing arrays to functions ties back to Chapter 6, since arrays are reference types and modifications inside a function are visible to the caller.
CSE110 arrays, Java array declaration, zero-based indexing, array last index off by one, ArrayIndexOutOfBoundsException, 2D array Java, multidimensional array, row column index, nested for loop 2D array, matrix iteration Java, array .length property, single type array constraint, ASU CSE110 arrays, Principles of Programming arrays