Duyệt Mảng
Đã giới thiệu| English | Ý nghĩa |
|---|---|
| traversal/træˈvɜːsl/ | duyệt |
| enhanced for loop/enˈhænst fɔː luːp/ | vòng lặp for tăng cường |
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.
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.

Traversing an array with an indexed for loop or an enhanced for loop
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.
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.
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.
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.
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.
Duyệt mảng để tính tổng
i truy cập từng index; sum tích lũy a[i] (ở đây a = {10,20,30}).
Điều kiện đúng để duyệt mảng a bằng chỉ số là...
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Các chỉ số hợp lệ là 0..length-1, nên i < a.length.
Nên dùng vòng lặp nào nếu cần MODIFICATION (thay đổi) các phần tử mảng?
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
for-each không thể gán lại; dùng a[i] = ... với vòng lặp có chỉ số.
Trong for (int x : a) { x = 0; }, mảng a được đặt thành toàn zeros.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
x là bản sao cục bộ; mảng không bị thay đổi.
Vòng lặp for-each for (int x : a) tốt nhất khi bạn chỉ cần...
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
for-each đọc giá trị sạch sẽ mà không cần chỉ số.
Truy cập từng phần tử của mảng được gọi là ___ (một từ).
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
A traversal (duyệt) truy cập từng phần tử.