Lesson 025

Two-Dimensional Arrays

Rows, Columns & Nested Loops

1:00

How to represent, access, traverse, and operate on grids using arrays of arrays, including the row-major memory model and common pitfalls.

By the end, you can

  • Explain what a 2D array is and how it relates to an array of arrays.
  • Access any cell using `grid[row][col]` and explain why row comes first.
  • Distinguish rectangular grids from jagged arrays and know when `grid[0].length` is unreliable.
  • Measure a grid's dimensions using `grid.length` and `grid[0].length`.
  • Create a grid safely with `Array.from` and explain why `Array(n).fill(array)` is a bug.
  • Write nested loops to traverse every cell and state the time complexity.
  • Compute the flat row-major memory offset for any `[row][col]` coordinate.
  • Implement search, row-sum, and column-sum operations on a 2D array.
  • Identify and avoid the out-of-bounds error by using correct loop bounds.
Up next in Linear Data Structures
Questions or feedback?