这几天写个坦克大战遇到一个问题 代码如下
missiles是ArrayList 集合的实例
public void paint(Graphics g) {
System.out.println("gamepanel paintComponent");
tank.drawMe(g);
for(Missile missile:missiles){
if( missile.isLife()){
missile.drawMe(g);
System.out.println("______________missile draw" + missiles.size());
}
if( !(missile.isLife())){
missiles.remove(missile);
System.out.println("_______________remove missile" + missiles.size());
}
}
}
}
运行后爆出一下异常
Exception in thread "GamePanel" java.util.ConcurrentModificationException
最后实在修改了大半天线程 发现都没用 最后给成下面代码 就没有问题了就是蛋疼
public void paint(Graphics g) {
System.out.println("gamepanel paintComponent");
tank.drawMe(g);
for(int i = 0; i< missiles.size();i++){
Missile missile = missiles.get(i);
if( missile.isLife()){
missile.drawMe(g);
System.out.println("______________missile draw" + missiles.size());
}
if( !(missile.isLife())){
missiles.remove(missile);
System.out.println("_______________remove missile" + missiles.size());
}
}
}
}
运行正常后突然感觉到 天晴了雨停了 我又觉得我ok了
最后发现
private class Itr implements Iterator<E> {
/**
* Index of element to be returned by subsequent call to next.
*/
int cursor = 0;
/**
* Index of element returned by most recent call to next or
* previous. Reset to -1 if this element is deleted by a call
* to remove.
*/
int lastRet = -1;
/**
* The modCount value that the iterator believes that the backing
* List should have. If this expectation is violated, the iterator
* has detected concurrent modification.
*/
int expectedModCount = modCount;
public boolean hasNext() {
return cursor != size();
}
public E next() {
checkForComodification();
try {
E next = get(cursor);
lastRet = cursor++;
return next;
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
public void remove() {
if (lastRet == -1)
throw new IllegalStateException();
checkForComodification();
try {
AbstractList.this.remove(lastRet);
if (lastRet < cursor)
cursor--;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException e) {
throw new ConcurrentModificationException();
}
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}