مروراتArrayList
مُقدَّم| English | المعنى |
|---|---|
| ConcurrentModificationException/kənˈkʌrənt ˌmɒdɪfɪˈkeɪʃn ekˈsepʃn/ | استثناء التعديل المتزامن |
Choose a traversal that matches the intended change
- An ArrayList contains [2, 3, 4]. You want to double every stored value, while keeping the number of elements unchanged.
- An indexed loop can use list.set(i, 2 * list.get(i)). Assigning x = 2*x in for (int x : list) changes only the local loop variable.
![Choose a traversal that matches the change: to double every value in [2, 3, 4], an indexed loop with list.set(i, 2 * list.get(i)) gives [4, 6, 8]; assigning x = 2 * x in an enhanced for loop changes only the local variable and leaves [2, 3, 4]](/learn-media/ap_computer_science_a/assets/0409-double-values.png?v=1794647927)
بدءاً من القائمة [2, 3, 4]، قم بتنفيذ for (int x : list) { x = 2 * x; }. ما هي قيمة list.get(1)?
الصعوبة 2/5 · 140 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
تعيين المتغير المحلي x لا يحل محل Integer المخزن. يظل العنصر الأوسط في القائمة 3.
Looping over a list
- Traverse an ArrayList just like an array, but with its methods. Indexed:
for (int i = 0; i < list.size(); i++) { ... list.get(i) ... } - For-each:
for (String s : list) { ... s ... }— clean for reading values.size()(notlength) bounds the indexed loop.
The same patterns
- Sum, count, min/max, and search all work the same as with arrays. Just swap
a[i]forlist.get(i)anda.lengthforlist.size(). - The accumulator and max/count logic is identical. Everything you learned for array traversal carries over.
Reading vs. modifying
- An enhanced loop copies each element value into its loop variable. For
Integerelements read asint x, assigning toxdoes not replace a list element; an indexed loop can replace it withset(i, x). - For structural changes (adding/removing), use the indexed loop carefully. Reading is simple; removing while looping needs extra care (next lesson).
جمعArrayList عن طريق الفهرس
حدود مع size()، قراءة مع get(i) (القائمة = [10,20,30]).
الشرط الصحيح لتكرار قائمةArrayList مؤشّرة هو...
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
تستخدمArrayList size(); بينما تستخدم المصفوفات length.
Why should a program avoid direct structural changes during an ArrayList enhanced-for traversal?
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
ArrayList fail-fast behaviour is best-effort. Avoid unsafe traversal rather than relying on a guaranteed exception.
أنماط مرور المصفوفة (المجموع، العد، الأقصى) تعمل بنفس الطريقة لـArrayList.
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
فقط استبدل a[i]/a.length بـ list.get(i)/list.size() .
للإزالة الآمنة للعناصر أثناء المرور فوق قائمة، يجب عليك استخدام...
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
لا يمكن لـfor-each تعديل البنية؛ ولكن الحلقة المؤشّرة تستطيع ذلك (بحذر).
بدون تعديل بنيوي أثناء التجول، يمكن للحلقة المحسّنة قراءة جميع المدخلات في مصفوفةArrayList
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
تقرأ الحلقة كل مرجع مخزن. التعديلات البنيوية أثناء التجول مسألة منفصلة؛ لا يضمن هذا الادعاء أن جميع العمليات على كل عنصر ممكن آمنة.
Don't modify during for-each
- Adding or removing during a for-each loop may trigger a ConcurrentModificationException 并发修改异常; fail-fast detection is best-effort. The for-each loop assumes the list doesn't change structure underneath it.
- To remove while traversing, use an indexed loop (and manage the index). Reading with for-each is suitable when the list is not structurally changed during traversal. Also check element requirements: unboxing a null Integer still throws an exception.
Bound an indexed ArrayList loop with list.size(), not .length, and never structurally modify during a for-each. Calling add/remove inside a for (x : list) may trigger a ConcurrentModificationException, but detection is not guaranteed. To delete while traversing, use an indexed loop — and remember remove(i) shifts later elements, so don't blindly i++ past the shifted element.
After the indexed doubling update, what is list.get(1)?
الصعوبة 2/5 · 140 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
The middle stored value 3 is replaced by 6. Reassigning a primitive loop variable would leave it at 3.
بالنسبة لمدخلات Student غير الفارغة، يمكن لاستدعاء s.addScore(5) داخل for (Student s : students) تغيير الطلاب المخزنين دون إضافة أو إزالة عناصر من القائمة.
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
المراجع المنسوخة تصل إلى كائنات Student قابلة للتعديل. يمكن لمجالاتها أن تتغير دون تغيير بنية القائمة.
Summing an ArrayList
- Indexed:
for (int i = 0; i < list.size(); i++) { sum += list.get(i); } - For-each:
for (int x : list) { sum += x; }(autoboxing/unboxing handles Integer↔int). - Both total the list; for-each is cleaner for read-only.
Carry the reasoning to a new case
- Adding or removing directly during enhanced traversal is unsafe; fail-fast exceptions are best-effort, not a guaranteed correctness check.
- A list of mutable Student objects can be traversed with
for (Student s : students) { s.addScore(5); }, assuming non-null entries. This changes each object's state without changing list structure; assigning a different reference toswould not replace the stored element.
Traverse an ArrayList with an indexed loop (i < list.size(), list.get(i)) or an enhanced loop that receives each element value. The array patterns (sum, count, min/max, search) all carry over. Avoid direct structural changes during an enhanced traversal: a ConcurrentModificationException may detect the bug, but is not guaranteed. Use a carefully managed indexed loop for the removal pattern taught next; changing a referenced object's fields is different from changing list structure.