Unit 4 Cycle 2 Day 3: ArrayList Remove During Iteration
Share
Unit 4, Data Collections • Cycle 2
Day 3 Advanced Practice • Harder Difficulty
Focus: ArrayList Remove During Iteration
Hard
ArrayList Remove During Iteration
Advanced Practice Question
Format: ArrayList Remove During Iteration
What is the output?
What is the output?
ArrayList list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
for (int i = 0; i < list.size(); i++) {
if (list.get(i) % 2 == 0) {
list.remove(i);
}
}
System.out.println(list);
Difficulty: Hard |
Topic: ArrayList Remove During Iteration |
Cycle: 2 (Advanced)
Why This Answer?
When 2 is removed at index 1, element 3 shifts to index 1. Loop continues to index 2, skipping 3. Element 4 at index 2 is checked and removed. Result: [1, 3].
Common Mistake
Watch Out!
Not accounting for index shifts when removing during iteration.
AP Exam Strategy
Removing during forward iteration skips elements. Use backward iteration: i--.
Master This Topic
This Cycle 2 HARD question tests arraylist remove during iteration. Review Unit 4 concepts to build mastery of data collections.
- Understanding arraylist remove during iteration
- Tracing code execution accurately
- Avoiding common pitfalls
Ready for More Challenges?
Cycle 2 questions prepare you for the hardest AP CSA exam questions.
Study Games Practice FRQs