Developing Algorithms Using ArrayLists

AP Computer Science A· difficulty 4/5

Removing elements while iterating with <code>for (int i = 0; i < list.size(); i++)</code> can skip elements because

  • A

    After remove(i), the next element shifts to index i but i is incremented before processing it

    check_circle
  • B

    ArrayList does not support remove() during iteration

  • C

    Java's garbage collector breaks the iterator

  • D

    list.size() always returns 0 after removal

Explanation

When list.remove(i) succeeds, all subsequent elements shift left by one. The loop's <code>i++</code> then jumps over the new element at i. Use <code>i--</code> after remove or iterate backwards.

Want 10 more like this — adaptive to your weak spots?

Related questions