foreach遍历时删除为什么会有异常

- 通过编译出来的字节码可以看出foreach方法是通过Iterator和while循环实现的。
- Iterator删除元素的方式
//通过角标和size对比判断是否有下一个
public boolean hasNext() {
return cursor != size;
}
@SuppressWarnings("unchecked")
public E next() {
//判断已修改的次数和期望修改次数
checkForComodification();
int i = cursor;
if (i >= size)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;
return (E) elementData[lastRet = i];
}
public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
ArrayList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
//移除元素之后,将期望修改次数和修改次数设为相等
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
- ArrayList的删除元素方式
public boolean remove(Object o) {
if (o == null) {
for (int index = 0; index < size; index++)
if (elementData[index] == null) {
fastRemove(index);
return true;
}
} else {
for (int index = 0; index < size; index++)
if (o.equals(elementData[index])) {
fastRemove(index);
return true;
}
}
return false;
}
private void fastRemove(int index) {
//增加修改次数
modCount++;
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // clear to let GC do its work
}
通过对比可看出:iterator删除元素之后,把修改次数和期待修改次数置为相等。但是ArrayList删除元素仅仅将操作次数加1。由于foreach是通过iterator和while实现的,当hasNext成立时,iterator会调用next()方法,此时就会检查操作次数和期待操作次数,就会报出异常。
假如说集合里边两个元素,我只删除第一个元素,那么foreach是不会报异常的。原因如下,foreach删除第一个元素,那么操作次数加1,while循环继续执行,判断hasNext()返回true或false,此时cursor位置为1,集合size为1,那么hasNext()返回false,不再执行next()取第二个元素了,也就不会判断期待操作和操作是否相等了,也就不会报异常了
那么正确的遍历删除怎么做
通过fori的方法
因为fori不是通过iterator和while实现的,所以不会判断操作次数和期待操作次数。虽然fori不会报错,但是也有需要注意的地方,如图所示:

虽然我们移除了等于3的元素,但是只移除了第一个,第二个3并没有移除。
这是因为for循环遍历到index=3,删除了第一个3,第二个3因此往前移了一位index=3,此时for循环即将进行index=4的操作,所以第二个3没有被删除。
正确的做法如下
#####通过Iterator方法

1700

被折叠的 条评论
为什么被折叠?



