A topic in the Open Knowledge Graph — a free, open map of 15,495 topics and the order to learn them in.

Two-Dimensional Arrays

College Depth 103 in the knowledge graph ☐ I know this ☆ Set as goal
228topics build on this
515prerequisites beneath it
See this on the map →
Array Indexing and BoundsArrays and Lists+2 more→→Graph Representations: Adjacency List vs. Adjacency Matrix
arrays 2d-arrays matrices grids nested-loops traversal

Core Idea

A two-dimensional array stores values in a grid of rows and columns, and each element is reached with two indexes: `grid[row][col]`. In Java and Python it is built as an array whose elements are themselves arrays (rows). Grids model game boards, images, seating charts, spreadsheets and matrices. Processing them almost always uses nested loops, with the outer loop choosing a row and the inner loop walking across its columns.

How It's Best Learned

Draw the grid on paper with row and column numbers before writing any code, and have students label `grid[1][2]` on the drawing. Practice the standard traversals in order: row-major (row by row), column-major (column by column), one row, one column, and the whole grid for a sum or maximum. In Python, show the shared-row bug from `[[0] * cols] * rows` on screen so students see why each row must be created separately.

Common Misconceptions

Explainer

You already know that an array stores a sequence of values reached by one index. Many problems need two directions instead of one. A tic-tac-toe board has 3 rows and 3 columns. A grayscale image has rows and columns of pixel brightness values. A two-dimensional array models these grids directly.

In Java you write `int[][] board = new int[3][4];`. This creates 3 rows with 4 columns each, and every `int` starts at 0. You reach one element with two indexes, row first: `board[1][2]` is row 1, column 2, counting from 0. In Python the usual form is a list of lists, such as `board = [[0, 0, 0, 0] for _ in range(3)]`, and access looks the same: `board[1][2]`.

It helps to know how the grid is built. A Java `int[][]` is really an array of row arrays. That explains the length rules. `board.length` is the number of rows, 3. `board[0].length` is the length of row 0, which is the number of columns, 4. Because rows are separate arrays, Java even allows rows of different lengths, called ragged arrays, though most problems use rectangles.

Processing a grid uses the nested loops you have already met. The standard row-major traversal is:

```java

for (int r = 0; r < board.length; r++) {

for (int c = 0; c < board[r].length; c++) {

// use board[r][c]

}

}

```

The outer loop chooses a row. The inner loop walks across it. Swap the loops and you get column-major order, which visits all of column 0 first. To work on just one row, fix `r` and loop over `c`. To work on one column, fix `c` and loop over `r`.

Common tasks follow directly from these patterns. A total adds every element. A row sum resets an accumulator at the start of each row. Finding the largest value tracks a running maximum. Checking a tic-tac-toe win looks at one row, one column, or a diagonal, where the diagonal elements are `board[i][i]`.

Watch the indexes carefully. The most common bug is swapping row and column. The second is using `board.length` as the column bound, which works only when the grid happens to be square. In Python, the third is building rows with `[[0] * cols] * rows`, which stores one row object several times.

Practice Questions 4 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Combining Small Groups Within 5 → Number Bonds Within 5 → Number Bonds to 10 → Tens and Ones Place Value → Reading and Writing Three-Digit Numbers → Comparing Three-Digit Numbers → Comparing and Ordering Three-Digit Quantities → Comparing and Ordering Whole Numbers → Comparing Numbers 1 to 20 → Ordering Numbers to 20 → Number Line 0 to 20 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Opposites and Additive Inverses → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Boolean Algebra and Fundamental Laws → Logic Gates Fundamentals → Implementing Boolean Functions with Gates → Karnaugh Map Simplification → Combinational Circuit Design → Flip-Flops and Latches → Finite State Machines (FSMs) → Deterministic Finite Automata (DFA) → Nondeterministic Finite Automata (NFA) → Regular Language Recognition Algorithms → DFA Properties and Minimization Algorithms → Regular Languages: Definition and Characterization → Context-Free Grammars (CFGs) → Context-Free Grammar Properties and Ambiguity → Parse Trees, Derivations, and Ambiguity in CFGs → Context-Free Grammars in Compiler Design → Abstract Syntax Trees (ASTs) → Symbol Tables and Scope Resolution → Semantic Analysis Phase → Scope and Binding Resolution → Scope, Shadowing, and Variable Lifetime → Parameter Passing: Value vs. Reference → Two-Dimensional Arrays

Longest path: 104 steps · 515 total prerequisite topics

Prerequisites (4)

Leads To (1)