remove elements from CopyOnWriteArrayList

前端 未结 9 1981
醉酒成梦
醉酒成梦 2020-12-29 09:27

I am getting an exception when I try to remove elements from CopyOnWriteArrayList using an iterator. I have noticed that it is documented

Element-c

9条回答
  •  -上瘾入骨i
    2020-12-29 10:20

    Something like this:

    int pos = 0;
    while(pos < lst.size() ) {
      Foo foo = lst.get(pos);
      if( hasToBeRemoved(foo) ) {
        lst.remove(pos);
        // do not move position
      } else {
        pos++;
      }
    }
    

提交回复
热议问题