تصفح المصفوفات ثنائية الأبعاد 2D
| English | العربية |
|---|---|
| nested loops/ˈnestɪd luːps/ | حلقات متداخلة |
| row-major order/rəʊ ˈmeɪdʒə ˈɔːdə/ | ترتيب الصفوف أولاً |
| enhanced for/enˈhænst fɔː/ | for معزز |
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.
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] ... } }The inner loop sweeps a full row before the outer moves to the next.
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
- An enhanced for 增强循环 loop over a 2-D array gives one row (a 1-D array) at a time.
for (int[] row : g) { for (int x : row) { ... x ... } } - The outer variable is a whole row; the inner walks that row's values. The primitive loop variable does not update stored cells when reassigned. The row variable is still a reference, so an indexed inner loop can modify
row[c].
المرور حسب الصفوف
حلقة خارجية للصفوف، داخلية للأعمدة: 1,2,3,4,5,6.
أي نمط معروض هنا يزور كل خلية في مصفوفة أعداد صحيحة متعددة الأبعاد 2 التي جميع إشارات الصفوف فيها ليست فارغة؟
خارجية للصفوف، داخلية للأعمدة.
يجب أن تكون الحلقة الخارجية لتتبع مصفوفة متعددة الأبعاد 2 بترتيب الصفوف محصورة بـ...
تستخدم الحلقة الخارجية للصف g.length؛ وتستخدم الحلقة الداخلية للخلية g[r].length، مما يعالج عدم تساوي أطوال الصفوف.
بالنسبة لشبكة 2 صفوف و3 أعمدة، كم عدد الخلايا التي تزورها الحلقة المتداخلة؟
الصفوف × الأعمدة = 2 × 3 = 6.
يتم المرور حسب الصفوف على الخلايا بالترتيب...
المرور حسب الصفوف = من اليسار إلى اليمين، من الأعلى إلى الأسفل.
في for (int[] row : g) ، المتغير row هو...
متغير الحلقة الخارجية for-each هو مصفوفة صف.
استخدام g.length لكلا حدود الحلقات يسبب دائمًا استثناءً على أي شبكة مستطيلة غير مربعة.
خطأ: في شبكة ذات 2 صفوف و3 أعمدة، يزور عمودين فقط ويتجاهل الخلايا بصمت. وفي الشبكة الطويلة أكثر من العرضية قد تتجاوز حدًا ما لصف.
Bounding correctly
- Outer bound:
r < g.length(rows). Inner bound:c < g[r].length(the current row's length). Using the wrong length for a loop is a subtle bug in non-square grids. - For a nonempty rectangular grid,
g[0].lengthalso gives the common column count, and the inner body runsrows × columnstimes. For ragged rows, add the row lengths; all row references must be non-null.
Use g.length for rows and g[r].length for the current row's cells. Using the row count as both bounds can skip columns in a wide grid or run out of bounds in a tall grid. An empty outer array needs no access to g[0]; a null row needs an explicit handling policy before reading its length.
Order the cells visited in row-major traversal of a 2×2 grid.
The inner loop finishes one row before the outer loop advances.
بالنسبة لمصفوفة ثنائية الأبعاد int[][] g = {{1, 2, 3}, {}, {4}};, كم عدد الخلايا التي يزورها التجول لكل صف؟
أضف أطوال الصفوف: 3 + 0 + 1 = 4. لا تساهم الصف الفارغ بأي خلايا.
Summing a 2-D array:
for (int r = 0; r < g.length; r++)for (int c = 0; c < g[r].length; c++)sum += g[r][c];— visits all cells row by row, assuming no row is null.
Carry the reasoning to a new case
- For
int[][] g = {{1, 2, 3}, {}, {4}};, the row lengths are 3, 0 and 1. The per-row bounds visit four cells and sum to 10;g[0].lengthis not a valid bound for every row. - You can modify row[c] with an indexed inner loop; reassigning a primitive x in an inner enhanced loop does not update a cell.
Traverse a 2-D array with nested loops: outer over rows (r < g.length), inner over columns (c < g[r].length), accessing g[r][c]. This visits cells in row-major order (the sum of row lengths; rows × columns for a rectangular grid). An enhanced for (for (int[] row : g)) hands a reference to one row at a time; assigning to a primitive inner-loop variable does not change a stored cell.