From the free course AP Computer Science A on Lykke

4.3 Array Creation and Access

Key concepts: One-dimensional and two-dimensional array creation · Two-dimensional array initializer lists · Array indexing and element access · Row-and-column indexing convention · Array element modification · Avoiding array out-of-bounds errors · Traversing arrays with loops · Accessing all elements of an array · Computing values from array elements · Method implementation using arrays

An array is a fixed-size sequence whose elements are accessed by position, and a two-dimensional array extends that idea by organizing elements into rows and columns.

4.3 Array Creation and Access

An array is a fixed-size sequence whose elements are accessed by position, and a two-dimensional array extends that idea by organizing elements into rows and columns.

One-dimensional arrays: one index, one position

A one-dimensional array stores elements in a single sequence. Java creates an array with the new keyword, followed by the element type and the number of elements:

int[] temperatures = new int[4];

This creates four integer locations. Because no values were supplied, each location initially contains Java’s default value for int, which is $0$. The valid indices are $0$, $1$, $2$, and $3$; the array’s length is temperatures.length, which is $4$.

temperatures[0] = 72;
temperatures[1] = 68;
temperatures[2] = 75;
temperatures[3] = 70;

int afternoon = temperatures[2];  // 75

An index identifies an element’s position. Java arrays begin indexing at $0$, not $1$.

Two-dimensional arrays: rows containing one-dimensional arrays

A two-dimensional array is accessed with two indices. Conceptually, each row is itself a one-dimensional array:

int[][] arr2D = {
    {1, 2, 3},
    {4, 5, 6}
};

The initializer is a list of one-dimensional array initializer lists. The outer braces contain two rows; each inner brace supplies the values in one row.

Expression Meaning Value
arr2D.length number of rows $2$
arr2D[0].length number of columns in row $0$ $3$
arr2D[1][0] row $1$, column $0$ $4$
arr2D[0][2] row $0$, column $2$ $3$

The exam convention is strict: in arr[first][second], the first index is the row and the second index is the column. Thus, arr2D[1][2] means “row $1$, column $2$,” producing $6$—not “row $2$, column `$1$].”

Creation, access, and modification

A two-dimensional array can also be created with dimensions rather than literal values:

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

This creates $3$ rows and $4$ columns. Every element initially contains $0$. An element can then be read or replaced using row-and-column indexing:

seats[1][2] = 7;
int reserved = seats[1][2];  // 7

The assignment changes the element stored at row $1$, column $2$; it does not change the array’s size.

Accessing every valid element

To visit every element, a loop must include every valid row and column index. Since valid indices stop at one less than the corresponding length, use <, not <=:

for (int row = 0; row < arr2D.length; row++) {
    for (int col = 0; col < arr2D[row].length; col++) {
        System.out.println(arr2D[row][col]);
    }
}

The outer loop selects a row. The inner loop selects each column within that row. Using arr2D[row].length makes the column bound belong to the row currently being accessed.

Bounds: the guardrail that prevents failure

For an array named values, valid row indices range from $0$ through values.length - 1. For a particular row, valid column indices range from $0$ through values[row].length - 1$. An invalid index causes an ArrayIndexOutOfBoundsException`.

Named misconception — “length is the last index.” If values.length is $5$, the last valid index is $4$, not $5$. The expression values[values.length] is out of bounds.

Named misconception — swapping row and column. In arr[row][col], the first index is not the column. Drawing the grid and labeling the row first prevents this frequent error.

A specified getPointsForRow trace provides a compact access-and-calculation check: the selected row contributes 200 + 300 + 100 + 200 + 200 = 1000; because every space has the same color, the value is doubled, giving 1000 × 2 = 2000. The expected return value is 2000.

Retrieval check

For int[][] grid = new int[2][3];, identify the valid index ranges and the element accessed by grid[1][2]. Then state why grid[2][0] fails.

Answer: Rows range from $0$ through $1$; columns range from $0$ through $2$. grid[1][2] is the last element in the second row. grid[2][0] fails because row $2$ does not exist.

4.3 Array Creation and Access - AP Computer Science A - image 1
4.3 Array Creation and Access - AP Computer Science A - image 1
4.3 Array Creation and Access - AP Computer Science A - diagram 1
4.3 Array Creation and Access - AP Computer Science A - diagram 1

Source Materials

The whole course on Lykke · Browse all public course wikis