Here is my code:
public class Test {
public static void main(String[] args){
ArrayList<Integer> list = new ArrayList();
list.add(1);
list.add(2);
list.add(2);
list.add(2);
list.add(5);
int inteval = 0;
Iterator<Integer> it = list.iterator();
for(;it.hasNext();){
Integer n = it.next();
list.remove(n);
if (list.contains(n-inteval)){
list.remove(list.indexOf(n-inteval));
if (list.contains(n-inteval-inteval)){
list.remove(list.indexOf(n-inteval-inteval));
}else{
list.add(n-inteval);
list.add(n);
}
}
}
}
}
this code will throw ConcurrentModificationException, I have tryed to use CopyOnWriteArrayList, but I found it.next() returns a value the removed by last time! how can I fixed it?