2D Array Traversals
แนะนำใหม่
13:31
การเก็บรวบรวมข้อมูล
ถ่ายรูปหนึ่งรูปบนโทรศัพท์ของคุณ. สำหรับคอมพิวเตอร์แล้วมันไม่ใช่รูปภาพเลย — มันคือตารางของตัวเลข, ตัวเลขหนึ่งสำหรับทุกพิกเซล, ประมาณสิบสองล้านตัว. ลองทำ…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| 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 |
การ์ดนี้เป็นการ์ดใหม่ จึงแสดงเป็นภาษาอังกฤษจนกว่าคำแปลจะพร้อม
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)

Nested loops for a grid
- To visit every cell of a 2-D array, use nested loops (from Unit 2). Outer loop over rows, inner loop over columns:
for (int r = 0; r < g.length; r++) { for (int c = 0; c < g[r].length; c++) { ... g[r][c] ... } }ลูปภายใน sweep แถวเต็มก่อนที่ลูปภายนอกจะย้ายไปยังแถวถัดไป
Row-major order
- This nesting visits cells in row-major order: all of row 0, then row 1, …
g[0][0], g[0][1], …, g[1][0], g[1][1], … - It's the natural reading order — left to right, top to bottom. For a nonempty rectangular grid, columns can instead be the outer loop to visit cells column by column. A ragged array needs an explicit policy for columns that some rows do not contain.
The for-each version
- enhanced for loop บน/array 2-D จะให้ แถว (array 1-D) ออกมาทีละแถว
for (int[] row : g) { for (int x : row) { ... x ... } } - ตัวแปรภายนอกคือทั้งแถว; ตัวแปรภายในจะวนผ่านค่าของแถวนั้น ตัวแปร primitive loop ไม่อัปเดตเซลล์ที่เก็บไว้เมื่อมีการกำหนดค่าใหม่ ตัวแปรแถวยังคงเป็น reference ดังนั้น nested loop แบบใช้ดัชนีสามารถแก้ไข
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(ความยาวของแถวปัจจุบัน) การใช้ความยาวผิดสำหรับ loop เป็นข้อผิดพลาดที่ซับซ้อนในตารางที่ไม่ใช่สี่เหลี่ยมจตุรัส - สำหรับตารางสี่เหลี่ยมไม่เต็มที่มีข้อมูล
g[0].lengthยังให้จำนวนคอลัมน์รวมด้วย และบอดี้ภายในจะวนrows × columnsครั้ง สำหรับแถวที่มีความยาวต่างกัน ให้บวกความยาวของแต่ละแถว; reference ของแถวทั้งหมดต้องไม่ใช่ null
ใช้ g.length สำหรับจำนวนแถวและ g[r].length สำหรับเซลล์ของแถวปัจจุบัน การใช้นับจำนวนแถวเป็นทั้งสองขอบเขตอาจทำให้ข้ามคอลัมน์ในตารางกว้าง หรือเกิด out of bounds ในตารางสูง Array ภายนอกว่างเปล่าไม่ต้องเข้าถึง 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 array:
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] ด้วย nested loop แบบใช้ดัชนี; การกำหนดค่า x แบบ primitive ใหม่ใน enhanced loop ภายในจะไม่อัปเดตเซลล์ที่เก็บไว้
traverses array 2-D ด้วย nested loops: ภายนอกวนตามแถว (r < g.length), ภายในวนตามคอลัมน์ (c < g[r].length), เข้าถึง g[r][c] สิ่งนี้จะเข้าถึงเซลล์ตามลำดับ row-major (ผลรวมของความยาวแถว; rows × columns สำหรับตารางสี่เหลี่ยม) enhanced for (for (int[] row : g)) ส่ง reference ของแถวหนึ่งต่อครั้ง; การ assign ตัวแปร primitive ของ loop ภายในจะไม่เปลี่ยนเซลล์ที่เก็บไว้
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้