MOBI BOOT CAMP CORP. logoLearning Buddy
  • SIGN IN
  • Introduction
  • Unit 0: The First Program
  • Unit 1: Using Objects and Methods
  • Unit 2: Selection and Iteration
  • Unit 3: Class Creation
  • Unit 4: Data Collections
    • Part 1: Arrays
    • Part 2: ArrayList
    • Part 3: 2D Arrays and Recursion
      • 2D Array Creation and Access
      • 2D Array Traversals
      • Implementing 2D Array Algorithms
      • Searching Algorithms
      • Sorting Algorithms
      • Recursion
      • Recursive Searching and Sorting
      • Unit 4 Part 3 Slides

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.

Array of Arrays
  • 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.

Diagram: 2D Array Structure - Logical Grid vs Memory View

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.

Zero-Initialization
  • Numeric types default to 0 or 0.0, boolean defaults to false, and reference types (like String) default to null.

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.

Nested Initializers
  • 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.

Row-Major Indexing
  • 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.

Row Extraction
  • 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.
Dimension Counting
  • Use the array name for row count and the first row's index for column count. Accessing indices outside 0 to length - 1 triggers an ArrayIndexOutOfBoundsException.

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
Privacy Policy | Terms & Conditions