On 01/07/2015 11:47 AM, Daniel Fuchs wrote:
On 07/01/15 11:31, Paul Sandoz wrote:

On Jan 7, 2015, at 10:45 AM, Remi Forax <[email protected]> wrote:

A simple Java question, what this code does ?

  ArrayList<String> list = new ArrayList<>();
  list.add("foo");
  list.add("bar");
  for(String s: list) {
    list.remove(s);
  }

:(


We could improve the best-effort basis by which ConcurrentModificationException is thrown by snapshotting the collection size on construction of the iterator (at the expense of an extra field, but that might pack into a spare 4 bytes due to alignment at least on 64 bits IIUC).

yes, note that AbstractList.Itr exhibit the same issue.
I fear that fixing this bug may break some programs.


Or call checkForComodification() when cursor == size in hasNext()?

No, you can't.
The contract of AbstractList.modCount is pretty clear about that:
https://docs.oracle.com/javase/7/docs/api/java/util/AbstractList.html#modCount
hasNext() can not call checkForComodification().


-- daniel

Rémi



Paul.

Rémi
tip: the bug lies in ArrayList.Itr.hasNext() (and AbstractList.Itr.hasNext()).




Reply via email to