On this page12 sections
  1. Prerequisites
  2. Learning Objectives
  3. 1. One-Dimensional (1-D)
  4. 2. Two-Dimensional (2-D)
  5. 3. Three-Dimensional (3-D)
  6. 4. Multidimensional: Any Depth
  7. 5. Building Grids Automatically
  8. Code Examples
  9. Common Beginner Mistakes
  10. Practice Questions
  11. Practice Answers
  12. Key Takeaways

Lecture 012: 1-D, 2-D, 3-D & Multidimensional Lists

Day5: Lists & Maps
Lecture012 of 057
Estimated video length12 min
TargetDart 3.12
LevelBeginner

A list can hold anything, including other lists. A list of lists makes a table with rows and columns (2-D), like a mark sheet or a chess board. Go one level deeper and you get 3-D: floors, rows and rooms in a building.

Contents

Prerequisites · Objectives · Lesson · Code Examples · Common Mistakes · Practice Questions · Answers · Takeaways


Prerequisites

Lecture 011 (lists).


Learning Objectives

  • Tell 1-D, 2-D and 3-D lists apart
  • Read and change items with two or three indexes
  • Find the number of rows and columns
  • Build a grid with List.generate
  • Avoid the shared-row trap with List.filled

1. One-Dimensional (1-D)

A normal list is 1-D: one row of values, one index.

dart
var scores = [10, 20, 30];
print(scores[1]);   // 20

2. Two-Dimensional (2-D)

A 2-D list is a list whose items are lists. Think of it as rows and columns:

dart
var grid = [
  [1, 2, 3],   // row 0
  [4, 5, 6],   // row 1
];
code
          col 0  col 1  col 2
row 0  →    1      2      3
row 1  →    4      5      6

The first index picks the row, the second picks the column:

dart
print(grid);           // [[1, 2, 3], [4, 5, 6]]
print(grid[0]);        // [1, 2, 3] (a whole row)
print(grid[1][2]);     // 6 (row 1, column 2)
print(grid.length);    // 2 (rows)
print(grid[0].length); // 3 (columns in row 0)

grid[0][0] = 100;
print(grid);           // [[100, 2, 3], [4, 5, 6]]

3. Three-Dimensional (3-D)

A 3-D list is a list of 2-D lists. Here, building[floor][row][room]:

dart
var building = [
  [                  // floor 0
    ['101', '102'],
    ['111', '112'],
  ],
  [                  // floor 1
    ['201', '202'],
    ['211', '212'],
  ],
];
print(building[1][0][1]);   // 202 (floor 1, row 0, room 1)
print(building.length);     // 2 floors

4. Multidimensional: Any Depth

You can keep nesting, with one index per level. In practice, 2-D is common and 3-D is rare. Deeper than that usually means a class (Day 11) would describe the data better.

dart
var deep = [[[[1]]]];
print(deep[0][0][0][0]);   // 1

5. Building Grids Automatically

List.generate(count, (index) => value) makes a list by running a small function for each index. (Functions like (row) => ... are explained on Days 10 and 14. For now, read (row) => row * col as "for each row, give back row * col".)

dart
var table = List.generate(3, (row) => List.generate(4, (col) => row * col));
print(table);   // [[0, 0, 0, 0], [0, 1, 2, 3], [0, 2, 4, 6]]

var board = List.generate(3, (_) => List.filled(3, '-'));
board[1][1] = 'X';
print(board);   // [[-, -, -], [-, X, -], [-, -, -]]

List.filled(3, '-') makes ['-', '-', '-']. The _ means "I don't need the index".


Code Examples

Example 1: A Class Mark Sheet

dart
void main() {
  // Each row is one student: [maths, science, english]
  var marks = [
    [78, 85, 90],
    [92, 88, 79],
    [65, 70, 72],
  ];
  var names = ['Ali', 'Sara', 'Hamza'];

  print('${names[0]} scored ${marks[0][1]} in science');
  print('${names[1]}: ${marks[1]}');
  print('${names[2]} total: ${marks[2][0] + marks[2][1] + marks[2][2]}');

  marks[2][0] = 68;
  print('After re-check, ${names[2]} maths: ${marks[2][0]}');
  print('Students: ${marks.length}, subjects: ${marks[0].length}');
}

Output

code
Ali scored 85 in science
Sara: [92, 88, 79]
Hamza total: 207
After re-check, Hamza maths: 68
Students: 3, subjects: 3

Row i belongs to names[i], so the two lists line up. On Day 8 you'll use loops to total every row without writing each index by hand.


Common Beginner Mistakes

1. A row that doesn't exist

dart
var grid = [
  [1, 2],
  [3, 4],
];
print(grid[2][0]);
code
Unhandled exception:
RangeError (length): Invalid value: Not in inclusive range 0..1: 2

Two rows means row indexes 0 and 1.

2. Mixing up row and column: grid[row][col], always row first. grid[1][2] is row 1, column 2, not the other way round.

3. The List.filled trap: every row is the same list

dart
var board = List.filled(2, List.filled(2, '-'));
board[0][0] = 'X';
print(board);

Output

code
[[X, -], [X, -]]

No error, but both rows changed! List.filled put the same inner list in every row. Use List.generate(2, (_) => List.filled(2, '-')) so each row is a new list.


Practice Questions

  1. In var g = [[1, 2], [3, 4], [5, 6]];, what is g[2][1]?
  2. How many rows and columns does g have?
  3. Write the line that changes the 3 in g to 30.
  4. In a 3-D list, how many indexes do you need to reach a single value?
  5. Why can List.filled(3, []) cause surprising results for a grid?

Practice Answers

  1. 6
  2. 3 rows (g.length), 2 columns (g[0].length).
  3. g[1][0] = 30;
  4. Three: list[a][b][c].
  5. Every row is the same list, so changing one row changes them all. Use List.generate.

Key Takeaways

  • 1-D = one list; 2-D = a list of lists (rows and columns); 3-D = a list of 2-D lists.
  • Read with one index per level: grid[row][col], building[floor][row][room].
  • grid.length is the rows; grid[0].length is the columns.
  • Build grids with List.generate, never with nested List.filled.