Unit 8: 2D Array

Traversing, accessing, and manipulating two-dimensional arrays

Unit Resources

Select a resource below to start studying.

📚Study Guide: 2D Array

Unit 8: 2D Array

Two-dimensional arrays extend the concept of arrays to store data in a grid or matrix format, with rows and columns. In Java, a 2D array is essentially an array of arrays, meaning each row is itself an array and can have a different length (ragged arrays), though the AP exam typically assumes rectangular arrays. Students must master nested loop traversal, understanding that the outer loop usually controls the row index and the inner loop controls the column index. Accessing elements uses two indices: arr[row][col]. The number of rows is arr.length; the number of columns in a rectangular array is arr[0].length. Common algorithms include row sums, column sums, finding max/min in the entire grid, diagonal traversal, and matrix transposition. The AP exam frequently presents free-response questions where students must implement methods that process 2D arrays, such as summing a row, averaging a column, or identifying elements that meet a condition. Understanding how to traverse only the main diagonal (where row == col) or the anti-diagonal is important. Students must also be able to initialize 2D arrays with literal values and understand that passing a 2D array to a method passes a reference to the array of row references.

Key Concepts

  • Declaration and Initialization: int[][] grid = new int[3][4]; creates 3 rows, 4 columns. int[][] grid = {{1,2,3},{4,5,6}}; initializes with literals. Ragged arrays: each row can have different lengths.
  • Indexing: grid[row][col]. Rows range 0 to grid.length-1. Columns range 0 to grid[0].length-1 (for rectangular arrays). Always check bounds before accessing.
  • Traversal Patterns: Row-major: outer loop rows, inner loop columns (most common). Column-major: outer loop columns, inner loop rows. Diagonal: loop with single index i, access [i][i].
  • Row and Column Operations: Sum a row by fixing the row index and looping over columns. Sum a column by fixing the column index and looping over rows. Finding row/column averages follows similar patterns.
  • Enhanced for with 2D Arrays: for (int[] row : grid) { for (int val : row) { ... } }. This reads each row as an int[] array, then each value in that row.

Vocabulary

  • Matrix: A rectangular arrangement of numbers or elements in rows and columns, represented as a 2D array.
  • Row-Major Order: Traversing a 2D array by completing all columns in a row before moving to the next row.
  • Column-Major Order: Traversing a 2D array by completing all rows in a column before moving to the next column.
  • Main Diagonal: The set of elements in a square matrix where the row index equals the column index (i.e., [0][0], [1][1], [2][2]).
  • Ragged Array: A 2D array in which each row has a different number of columns; also known as a jagged array.
  • Transpose: An operation that flips a matrix over its main diagonal, swapping rows and columns.

Essential Formulas / Syntax Patterns

  • int rows = grid.length;
  • int cols = grid[0].length; // rectangular only
  • for (int r = 0; r < grid.length; r++) { for (int c = 0; c < grid[0].length; c++) { ... } }
  • for (int r = 0; r < grid.length; r++) { // sum row r int sum = 0; for (int c = 0; c < grid[r].length; c++) sum += grid[r][c]; }

Common Mistakes

  • Reversing Row and Column Indices: grid[row][col] is correct. grid[col][row] is only correct if you intentionally want to transpose or access column-major.
  • Assuming All Rows Have Same Length: grid[0].length works for rectangular arrays but may fail for ragged arrays. Use grid[r].length inside the loop for safety.
  • Off-By-One in Nested Loops: Ensure both loop conditions use <, not <=, when comparing to length. The last valid index is length - 1.
  • Forgetting 2D Arrays Are Arrays of Arrays: grid.length is the number of rows. Each row is an array. You can swap entire rows by swapping row references: int[] temp = grid[i]; grid[i] = grid[j]; grid[j] = temp;

AP Exam Strategies

  • Draw the Grid: Sketch the 2D array with row and column indices labeled before writing or tracing code.
  • Identify Traversal Order: Determine whether row-major, column-major, or diagonal traversal is needed before writing loops.
  • Check Rectangular Assumption: If the problem states "rectangular array," you can safely use grid[0].length for column count. Otherwise, use grid[r].length.
  • Isolate Row or Column Logic: For row operations, fix the row index and vary the column. For column operations, fix the column and vary the row. Don't mix them up.

Real-World Applications

  • Spreadsheets: Excel and Google Sheets are essentially 2D arrays of cells with formulas, formatting, and references.
  • Digital Images: A grayscale image is a 2D array of pixel intensities; color images use 3D arrays (width x height x channels).
  • Game Boards: Chess, checkers, and tic-tac-toe represent board states as 2D arrays where each cell holds a piece or empty marker.

Practice Quiz: 2D Array

Answer each question one at a time. Click an option to select your answer.

Question 1 of 150
Question
Loading...
Click to flip
Answer
Loading...
Click to flip back 🔀 Shuffle
1 / 42

🎥Free Video Lessons: 2D Array

Watch these unit review videos directly on our site.

AP Computer Science A Unit 8 Review - 2D Arrays - Rows & Columns - Traverse - Sort - Search - CSA by Meek Extra Help

APCS Unit 8 (Part 2): 2D Arrays In-Depth Review and Practice Test by AP Computer Science A

AP Computer Science A - Unit 8: 2D Arrays by Bill Barnum

📄Cheat Sheet: 2D Array

Quick reference for 2D Array. Print this out and review before the exam!

Unit 8: 2D Array - Cheat Sheet

Declaration and Dimensions

  • int[][] grid = new int[rows][cols];
  • int rows = grid.length;
  • int cols = grid[0].length; // rectangular

Traversal Patterns

  • Row-major: outer = row, inner = column
  • Column-major: outer = column, inner = row
  • Diagonal: for (int i = 0; i < n; i++) grid[i][i]

Common Operations

  • Row sum: fix row, loop columns
  • Column sum: fix column, loop rows
  • Max/Min: nested loops compare all
  • Transpose: swap rows and columns

Enhanced For

  • for (int[] row : grid) { for (int val : row) { ... } }

Problem-Solving Quick Reference

  • grid[row][col], not grid[col][row]
  • Use grid[r].length for ragged arrays
  • Swapping rows: swap array references
  • Check both dimensions for bounds

🔬Ultimate Review Packet Materials

Download official review materials for this unit.

No URP materials available for this unit yet.

Check back soon for study guides, practice questions, and review videos.

← Back to AP Computer Science A