2D Array Traversals
導入済み
13:31
データコレクション
Take one photo on your phone. To the computer it is not a picture at all — it is a grid of numbers, one for every pixel, about twelve million of them. Now try…
英語のナレーション・英語と中国語の字幕を切り替え可能
このレッスンの内容
| English | 意味 |
|---|---|
| loop/luːp/ | A structure that repeats a sequence of instructions. |
| nested loops/ˈnestɪd luːps/ | ネストループ |
| 2-D array/tuː diː əˈreɪ/ | An array structure whose elements are accessed using two indices; Java represents it using arrays whose elements refer to arrays. |
| inner loop/ˈɪnə luːp/ | A loop placed inside another loop’s body. |
| row-major order/rəʊ ˈmeɪdʒə ˈɔːdə/ | 行主順 |
| enhanced for/enˈhænst fɔː/ | 強化for文 |
| outer array | |
| index/ˈɪndeks/ | A position marker or lookup structure used to locate data within a collection or database. |
このカードは新しいため、翻訳ができるまで英語で表示されます。
A rectangular grid exposes the wrong loop bound
- A grid has two rows and three columns. A loop bounded by g.length for both indices 索引 visits only the first two columns.
- Use r < g.length and, for each row, c < g[r].length. This also handles rows of different lengths, while g[0].length assumes a nonempty rectangular grid.
![A rectangular grid exposes the wrong loop bound: a grid with two rows and three columns, looped with g.length for both indices, visits only the first two columns; use r < g.length and c < g[r].length](/learn-media/ap_computer_science_a/assets/0412-grid-bounds.png?v=1794225445)

グリッドのためのネストループ
- 2-D配列のすべてのセルを訪問するには、ネストループ(Unit 2より)を使用します。外側ループで行、内側ループで列を反復します:
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配列に対する強化forループは、一度に**1つの行(1-D配列)**を与えます。
for (int[] row : g) { for (int x : row) { ... x ... } } - 外側の変数は完全な行であり、内側ループはその行の値を.walkします。基本型ループ変数は再割り当て時に格納されたセルを更新しません。行変数はまだ参照であるため、インデックス付き内側ループで
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 を使用します。 行数を上限として使うと、広いグリッドでは列をスキップしたり、高いグリッドでは配列の bounds を超えたりする可能性があります。空の外部配列は 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次元配列の合計計算:
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 です。行ごとの bounds で访问されるセルは4つあり、合計は 10 になります。g[0].lengthは各行に対して有効な bounds ではありません。- 内側ループでインデックス指定して row[c] を変更できますが、enhanced for ループ内で primitive 型変数 x に再割り当てしても、セルの内容は更新されません。
2 次元配列をネストループで遍历します:外側ループは行(r < g.length)、内側ループは列(c < g[r].length)に対応し、g[r][c] にアクセスします。これは行主順序でセルを访问し、行長の総和(直方グリッドの場合は rows × columns)となります。enhanced for(for (int[] row : g))は一度に1つの行への参照を渡しますが、内側ループの primitive 型変数に代入しても格納されたセルは変わりません。
何が問題でしたか?この内容はサイトの運営チームだけが確認します。