概述
异常:java.util.ConcurrentModificationException: null
原因:Itr里面的expectedModCount会与ArrayList的modCount进行比较,二者不相等,所以会抛错。删除的时候是通过ArrayList的remove方法去操作的,不是Itr内部的那个删除方法去操作的。
解决方法:
1.使用itr删除
import java.util.ArrayList;
class MyTest{
public static void main(String[] args){
ArrayList<Integer> arr = new ArrayList<Integer>();
for(int i=0; i<10; i++){
arr.add(i);
}
for(Iterator<Integer> it=arr.iterator(); it.hasNext();){
Integer i = it.next();
if(i == 5){
it.remove();
}
else{
System.out.println(i);
}
}
}
}
2.每次删除的时候都让i减1
import java.util.ArrayList;
class MyTest{
public static void main(String[] args){
ArrayList<Integer> arr = new ArrayList<Integer>();
for(int i=0; i<10; i++){
arr.add(i);
}
for(int i=0; i<arr.size(); i++){
if(i == 5){
arr.remove(i);
i -= 1;
}
else{
System.out.println(i);
}
}
}
}
3.倒序删除,先遍历一遍记录要删除的下标,然后从末尾开始删除
报错源码位置:ArrayList.java里的Itr.next()和Itr.checkForComodification()。
报错就是在Itr.next()中调用checkForComodification()以后产生的,在checkForComodification()函数中,判断了modCount和expectedModCount是否相等。如果不相等,就会抛出我们遇到的这个异常。
报错源代码:
... ...
@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];
}
... ...
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
底层源代码分析:
这里的Itr是在ArrayList的内部类,实现了Iterator接口,用于遍历。
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
... ...
/**
* An optimized version of AbstractList.Itr
*/
private class Itr implements Iterator<E> {
int cursor; // index of next element to return
int lastRet = -1; // index of last element returned; -1 if no such
int expectedModCount = modCount;
// prevent creating a synthetic constructor
Itr() {}
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();
}
}
@Override
public void forEachRemaining(Consumer<? super E> action) {
Objects.requireNonNull(action);
final int size = ArrayList.this.size;
int i = cursor;
if (i < size) {
final Object[] es = elementData;
if (i >= es.length)
throw new ConcurrentModificationException();
for (; i < size && modCount == expectedModCount; i++)
action.accept(elementAt(es, i));
// update once at end to reduce heap write traffic
cursor = i;
lastRet = i - 1;
checkForComodification();
}
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
... ...
remove调用的源码
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
... ...
public boolean remove(Object o) {
final Object[] es = elementData;
final int size = this.size;
int i = 0;
found: {
if (o == null) {
for (; i < size; i++)
if (es[i] == null)
break found;
} else {
for (; i < size; i++)
if (o.equals(es[i]))
break found;
}
return false;
}
fastRemove(es, i);
return true;
}
private void fastRemove(Object[] es, int i) {
modCount++;
final int newSize;
if ((newSize = size - 1) > i)
System.arraycopy(es, i + 1, es, i, newSize - i);
es[size = newSize] = null;
}
... ...
remove()函数会调用fastRemove()函数,使得modCount的值自增1.
然而,for循环下一次调用Itr.next(),Itr.next()调用Itr.checkForComodification()时,会发现,modCount和expectedModCount两个值不相等!因为在这个删除操作的过程中没有对expectedModCount重新赋值,所以就抛出异常了。
参考链接:https://blog.csdn.net/qq_35056292/article/details/79751233
最后
以上就是勤恳大白为你收集整理的【java】报错java.util.ConcurrentModificationException: null的全部内容,希望文章能够帮你解决【java】报错java.util.ConcurrentModificationException: null所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复