2D Array Traversals
引入
13:31
数据集合
用手机拍一张照片。对计算机来说,这根本不像图片——它是一个数字网格,每个像素对应一个数字,大约有一千二百万个。现在尝试…
英文讲解 · 可切换中英文字幕
本课内容
| English | 释义 | 拼音 |
|---|---|---|
| loop/luːp/ | 循环 | xún huán |
| nested loops/ˈnestɪd luːps/ | 嵌套循环 | qiàn tào xún huán |
| 2-D array/tuː diː əˈreɪ/ | 二维数组 | èr wéi shù zǔ |
| inner loop/ˈɪnə luːp/ | 内层循环 | nèi céng xún huán |
| row-major order/rəʊ ˈmeɪdʒə ˈɔːdə/ | 行主序 | xíng zhǔ xù |
| enhanced for/enˈhænst fɔː/ | 增强循环 | zēng qiáng xún huán |
| outer array | 外层数组 | wài céng shù zǔ |
矩形网格暴露了错误的循环边界
- 该网格有两行三列。若两个索引均使用 g.length 作为循环边界,则只会遍历前两列。
- 应使用 r < g.length,且对每一行使用 c < g[r].length。此方法也能处理长度不等的行,而 g[0].length 假设网格是非空的矩形网格。
![矩形网格暴露了错误的循环边界:一个两行三列的网格,两个索引均使用 g.length 作为循环边界时,只会遍历前两列;应使用 r < g.length 和 c < g[r].length](/learn-media/ap_computer_science_a/assets/0412-grid-bounds.png?v=1794225445)

用嵌套循环处理网格
- 要遍历所有单元格的 2-D 数组,请使用嵌套循环(来自单元 2)。外层循环遍历 rows 行,内层循环遍历 columns 列:
for (int r = 0; r < g.length; r++) { for (int c = 0; c < g[r].length; c++) { ... g[r][c] ... } }内层循环在扫描完整行之前,外层不会移动到下一行。
行主序
- 这种嵌套按 row-major order 行主序访问单元格:先访问第 0 行的所有元素,然后是第 1 行,依此类推至
g[0][0], g[0][1], …, g[1][0], g[1][1], …。 - 这是自然的阅读顺序——从左到右,从上到下。对于非空矩形网格,也可以将列作为外层循环以逐列访问单元格。不规则数组需要对某些行不包含的列制定明确策略。
for-each 版本
- 对 2-D 数组进行 enhanced for 增强循环时,每次迭代给出一个 行(即一个 1-D 数组)。
for (int[] row : g) { for (int x : row) { ... x ... } } - 外层变量是一整行;内层遍历该行的值。基本类型循环变量在重新赋值时不会更新存储的单元格。由于行变量仍是引用,因此基于索引的内层循环可以修改
row[c]。
Row-major traversal
Outer loop over rows, inner over columns: 1,2,3,4,5,6.
Which pattern shown here visits every cell of a non-null 2-D int array whose row references are all non-null?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Outer over rows, inner over columns.
The outer loop of the shown row-major 2-D traversal should be bounded by...
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
The outer row loop uses g.length; the inner cell loop uses g[r].length, which handles unequal row lengths.
For a 2-row, 3-column grid, how many cells does the nested loop visit?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
rows × columns = 2 × 3 = 6.
Row-major traversal visits cells in the order...
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Row-major = left to right, top to bottom.
In for (int[] row : g), the variable row is...
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
The outer for-each variable is a row array.
Using g.length for both loop bounds always throws an exception on any non-square rectangular grid.
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
False: in a 2-row, 3-column grid it visits only two columns and silently misses cells. In a taller-than-wide grid it can instead run past a row boundary.
正确设置循环边界
- 外层边界:
r < g.length(行数)。内层边界:c < g[r].length(当前行的长度)。在非方形网格中,为循环使用错误的长度是一种隐蔽的错误。 - 对于非空矩形网格,
g[0].length也给出各行共同的列数,内层循环体运行rows × columns次。对于不规则数组的行,需累加各行长度;所有行引用都不能为 null。
使用 g.length 作为行数,使用 g[r].length 作为当前行的单元格数。 同时使用行数作为两个边界会导致宽网格跳过某些列,或在高网格中出现越界错误。空的外层数组不需要访问 g[0];若某行为 null,则在读取其长度之前需要明确的处理策略。
Order the cells visited in row-major traversal of a 2×2 grid.
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
The inner loop finishes one row before the outer loop advances.
For int[][] g = {{1, 2, 3}, {}, {4}};, how many cells does the per-row traversal visit?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
Add the row lengths: 3 + 0 + 1 = 4. The empty row contributes no cells.
对 2-D 数组求和:
for (int r = 0; r < g.length; r++)for (int c = 0; c < g[r].length; c++)sum += g[r][c];—— 在没有行为 null 的前提下,逐行访问所有单元格。
将推理应用于新情况
- 对于
int[][] g = {{1, 2, 3}, {}, {4}};,行长度分别为 3、0 和 1。使用每行各自的边界会访问四个单元格,求得的总和为 10;g[0].length并非适用于每一行的有效边界。 - 可以使用基于索引的内层循环修改 row[c];在内层增强循环中重新赋值基本类型变量 x 不会更新单元格。
使用嵌套循环遍历2维数组:外层循环遍历行(r < g.length),内层循环遍历列(c < g[r].length),访问元素g[r][c]。这按行优先顺序访问单元格(即行长度之和;对于矩形网格,rows × columns)。增强型for循环(for (int[] row : g))每次传递一行的引用;将值赋给基本类型的内层循环变量不会改变已存储的单元格。
出了什么问题?只有网站团队能看到这些信息。