Array Traversals · 数组遍历
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| traversal/træˈvɜːsl/ | 遍历 | biàn lì |
| enhanced for loop/enˈhænst fɔː luːp/ | 增强for循环 | zēng qiáng for xún huán |
Visiting every element
- A traversal 遍历 visits every element of an array, usually in order.
- The standard indexed loop:
for (int i = 0; i < a.length; i++) { ... a[i] ... } isteps from0tolength - 1, soa[i]reaches each element once.- Traversal is the foundation of every array algorithm.
访问每个元素
- 遍历访问数组的每个元素,通常按顺序。
- 标准的带下标循环:
for (int i = 0; i < a.length; i++) { ... a[i] ... } i从0步进到length - 1,所以a[i]每个元素触及一次。- 遍历是每个数组算法的基础。
The for-each loop
- The enhanced for loop (for-each) 增强for循环 visits each value without an index:
for (int x : a) { ... x ... }—xtakes each element's value in turn.- Cleaner when you only need the values, not their positions.
- But it can't change the array or use the index.
for-each 循环
- 增强 for 循环(for-each)无需下标就访问每个值:
for (int x : a) { ... x ... }——x依次取每个元素的值。- 当你只需要值、而非它们的位置时更简洁。
- 但它不能改变数组或使用下标。
Indexed vs. for-each
- Use the indexed loop when you need the position
i, or to modify elements. - Use the for-each loop when you just need to read each value.
a[i] = 0;needs the indexed loop; summing values can use for-each.- Both visit every element — pick the one that fits the task.
带下标对 for-each
- 当你需要位置
i、或要修改元素时用带下标循环。 - 当你只需要读取每个值时用 for-each 循环。
a[i] = 0;需要带下标循环;对值求和可以用 for-each。- 两者都访问每个元素——选适合任务的那个。
Bounds again
- The indexed loop's condition
i < a.lengthkeepsivalid. i <= a.lengthoverruns by one → ArrayIndexOutOfBoundsException.- The for-each loop can't go out of bounds — it handles the range for you.
- When you write the index yourself, you own the bounds.
又是边界
- 带下标循环的条件
i < a.length让i保持有效。 i <= a.length会越过一步 → ArrayIndexOutOfBoundsException。- for-each 循环不会越界——它替你处理范围。
- 当你自己写下标时,边界由你负责。
The for-each loop reads values but can't assign back into the array. for (int x : a) { x = 0; } changes only the local copy x, not a — to zero out the array you need the indexed loop a[i] = 0;. Use for-each to read, the indexed loop to modify or when you need the position i.
for-each 循环读取值,但不能赋值回数组。for (int x : a) { x = 0; } 只改变局部副本 x,而非 a——要把数组清零你需要带下标循环 a[i] = 0;。用 for-each 去读,用带下标循环去修改、或当你需要位置 i 时。
Two ways to sum an array:
- Indexed:
for (int i = 0; i < a.length; i++) { sum += a[i]; } - For-each:
for (int x : a) { sum += x; } - Both give the same total; for-each is cleaner when the index isn't needed.
对数组求和的两种方式:
- 带下标:
for (int i = 0; i < a.length; i++) { sum += a[i]; } - for-each:
for (int x : a) { sum += x; } - 两者给出相同的总和;不需要下标时 for-each 更简洁。
A traversal visits every element. The indexed loop (for (int i = 0; i < a.length; i++)) gives the position i and can modify a[i]; the for-each loop (for (int x : a)) cleanly reads each value but can't change the array or use an index. Choose by whether you need the index or to modify.
遍历访问每个元素。带下标循环(for (int i = 0; i < a.length; i++))给出位置 i 并能修改 a[i];for-each 循环(for (int x : a))简洁地读取每个值,但不能改变数组或使用下标。按你是否需要下标或修改来选择。
Traversing an array to sum it · 遍历数组求和
i visits each index; sum accumulates a[i] (here a = {10,20,30}). · i 访问每个下标;sum 累加 a[i](这里 a = {10,20,30})。
The correct condition to traverse an array a with an index is... · 用下标遍历数组 a 的正确条件是……
Valid indices are 0..length-1, so i < a.length. · 有效下标是 0..length-1,所以 i < a.length。
Which loop should you use if you need to MODIFY the array elements? · 如果你需要修改数组元素,应该用哪种循环?
for-each can't assign back; use a[i] = ... with the indexed loop. · for-each 不能赋值回;用带下标循环的 a[i] = ...。
In for (int x : a) { x = 0; }, the array a is set to all zeros. · 在 for (int x : a) { x = 0; } 中,数组 a 被设为全零。
x is a local copy; the array is unchanged. · x 是局部副本;数组不变。
The for-each loop for (int x : a) is best when you only need to... · for-each 循环 for (int x : a) 最适合当你只需要……时。
for-each cleanly reads values without an index. · for-each 无需下标就简洁地读取值。
Visiting every element of an array is called a ___ (one word). · 访问数组的每个元素叫做一次 ___(填英文一词 traversal)。
A traversal visits each element. · 遍历访问每个元素。