On this page12 sections
Lecture 012: 1-D, 2-D, 3-D & Multidimensional Lists
| Day | 5: Lists & Maps |
| Lecture | 012 of 057 |
| Estimated video length | 12 min |
| Target | Dart 3.12 |
| Level | Beginner |
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.
var scores = [10, 20, 30];
print(scores[1]); // 202. Two-Dimensional (2-D)
A 2-D list is a list whose items are lists. Think of it as rows and columns:
var grid = [
[1, 2, 3], // row 0
[4, 5, 6], // row 1
]; col 0 col 1 col 2
row 0 → 1 2 3
row 1 → 4 5 6The first index picks the row, the second picks the column:
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]:
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 floors4. 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.
var deep = [[[[1]]]];
print(deep[0][0][0][0]); // 15. 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".)
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
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
Ali scored 85 in science
Sara: [92, 88, 79]
Hamza total: 207
After re-check, Hamza maths: 68
Students: 3, subjects: 3Row 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
var grid = [
[1, 2],
[3, 4],
];
print(grid[2][0]);Unhandled exception:
RangeError (length): Invalid value: Not in inclusive range 0..1: 2Two 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
var board = List.filled(2, List.filled(2, '-'));
board[0][0] = 'X';
print(board);Output
[[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
- In
var g = [[1, 2], [3, 4], [5, 6]];, what isg[2][1]? - How many rows and columns does
ghave? - Write the line that changes the
3ingto30. - In a 3-D list, how many indexes do you need to reach a single value?
- Why can
List.filled(3, [])cause surprising results for a grid?
Practice Answers
6- 3 rows (
g.length), 2 columns (g[0].length). g[1][0] = 30;- Three:
list[a][b][c]. - 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.lengthis the rows;grid[0].lengthis the columns.- Build grids with
List.generate, never with nestedList.filled.