Unit 4.11: 2D Array Creation and Access
2D Arrays
A 2D array is stored as an array of arrays. You can think of it as a grid or table with rows and columns. Its size (number of rows and columns) is established at creation and cannot be changed.
- In Java, a 2D array is technically a 1D array where each element is another 1D array (a row).
Exclusion Statement: Nonrectangular 2D array objects (jagged arrays) are outside the scope of the AP Computer Science A course and exam.
// EXAMPLE ONLY (Out of Scope): A jagged array has rows of different lengths
int[][] jagged = {
{1, 2, 3},
{4, 5},
{6, 7, 8, 9}
};
Visualizing Rectangular Arrays
Standard 2D arrays in APCSA are rectangular, meaning every row has the same number of columns.
Task: Creating a 2D array and setting specific values.
// In the main method ...
// Creates a 2D array with 3 rows and 3 columns
int[][] grid = new int[3][3];
// Setting values:
grid[1][1] = 5;
grid[2][2] = 99;
Default Values
When a 2D array is created using the new keyword, all its elements are initialized to their default values based on the data type.
- Numeric types default to
0or0.0,booleandefaults tofalse, and reference types (likeString) default tonull.
Task: Checking the default initialization value of an int 2D array.
// In the main method ...
// All 12 elements (3x4) are initialized to 0.
int[][] matrix = new int[3][4];
System.out.println(matrix[0][0]); // Prints 0
Initializer Lists
A 2D array can be created and initialized in one step using nested initializer lists.
- Use outer curly braces for the table and inner curly braces for each individual row.
Task: Creating and initializing a 2D array in one step.
// In the main method ...
// Creates and initializes a 2x3 2D array
int[][] matrix = {
{1, 2, 3}, // Row 0
{4, 5, 6} // Row 1
};
Accessing Elements
Elements in a 2D array are accessed and modified using [row][col] indices.
- The first index always refers to the row, and the second index refers to the column.
Task: Accessing and modifying elements using row and column indices.
// In the main method ...
int[][] matrix = {{1, 2, 3}, {4, 5, 6}};
// Access row 1, column 2 (value is 6)
int val = matrix[1][2];
// Modify row 0, column 0
matrix[0][0] = 10;
Accessing a Row
Because a 2D array is an "array of arrays," using a single index allows you to extract an entire row.
- Accessing a 2D array with only one set of brackets returns a reference to a 1D array (the specific row).
Task: Extracting a single row from a 2D array as a 1D array.
// In the main method ...
int[][] matrix = {{10, 20, 30}, {40, 50, 60}};
// 'myRow' is a 1D array reference pointing to {10, 20, 30}
int[] myRow = matrix[0];
System.out.println(myRow[1]); // Prints 20
Dimensions and Bounds
- Rows: The number of rows is
array.length. - Columns: The number of columns is
array[0].length.
- Use the array name for row count and the first row's index for column count. Accessing indices outside
0tolength - 1triggers anArrayIndexOutOfBoundsException.
Task: Getting the number of rows and columns using the length attribute.
// In the main method ...
String[][] seating = new String[5][8];
int rows = seating.length; // 5
int cols = seating[0].length; // 8
// seating[5][0] would throw ArrayIndexOutOfBoundsException