遍历集合出现的问题之前遇到对List进行遍历删除的时候,出现来一个ConcurrentModificationException 异常,可能好多人都知道list遍历不能直接进行删除操作,但是你可能只是跟我一样知道结果,但是不知道为什么不能删
之前遇到对List进行遍历删除的时候,出现来一个ConcurrentModificationException 异常,可能好多人都知道list遍历不能直接进行删除操作,但是你可能只是跟我一样知道结果,但是不知道为什么不能删除,或者说这个报错是如何产生的,那么我们今天就来研究一下。
我们先看下这段代码,你有没有写过类似的代码
publicstaticvoidmain(String[] args){ List<Integer> list = new ArrayList<>(); System.out.println("开始添加元素 size:" + list.size()); for (int i = 0; i < 100; i++) { list.add(i + 1); } System.out.println("元素添加结束 size:" + list.size()); Iterator<Integer> iterator = list.iterator(); while (iterator.hasNext()) { Integer next = iterator.next(); if (next % 5 == 0) { list.remove(next); } } System.out.println("执行结束 size:" + list.size());}
「毫无疑问,执行这段代码之后,必然报错,我们看下报错信息。」
我们可以通过错误信息可以看到,具体的错误是在checkForComodification 这个方法产生的。
首先我们看下ArrayList的iterator这个方法,通过源码可以发现,其实这个返回的是ArrayList内部类的一个实例对象。
public Iterator<E> iterator() { returnnew Itr();}
我们看下Itr类的全部实现。
privateclassItrimplementsIterator<E> { int cursor; // index of next element to returnint lastRet = -1; // index of last element returned; -1 if no suchint expectedModCount = modCount; Itr() {} publicbooleanhasNext(){ return cursor != size; } @SuppressWarnings("unchecked") public E next(){ checkForComodification(); int i = cursor; if (i >= size) thrownew NoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) thrownew ConcurrentModificationException(); cursor = i + 1; return (E) elementData[lastRet = i]; } publicvoidremove(){ if (lastRet < 0) thrownew IllegalStateException(); checkForComodification(); try { ArrayList.this.remove(lastRet); cursor = lastRet; lastRet = -1; expectedModCount = modCount; } catch (IndexOutOfBoundsException ex) { thrownew ConcurrentModificationException(); } } @Override@SuppressWarnings("unchecked") publicvoidforEachRemaining(Consumer<? super E> consumer){ Objects.requireNonNull(consumer); finalint size = ArrayList.this.size; int i = cursor; if (i >= size) { return; } final Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) { thrownew ConcurrentModificationException(); } while (i != size && modCount == expectedModCount) { consumer.accept((E) elementData[i++]); } // update once at end of iteration to reduce heap write traffic cursor = i; lastRet = i - 1; checkForComodification(); } finalvoidcheckForComodification(){ if (modCount != expectedModCount) thrownew ConcurrentModificationException(); }}
「参数说明:」
cursor : 下一次访问的索引;
lastRet :上一次访问的索引;
expectedModCount :对ArrayList修改次数的期望值,初始值为modCount;
modCount : 它是AbstractList的一个成员变量,表示ArrayList的修改次数,通过add和remove方法可以看出;
「几个常用方法:」
hasNext():
publicbooleanhasNext(){ return cursor != size;}
如果下一个访问元素的下标不等于size,那么就表示还有元素可以访问,如果下一个访问的元素下标等于size,那么表示后面已经没有可供访问的元素。因为最后一个元素的下标是size()-1,所以当访问下标等于size的时候必定没有元素可供访问。
next():
public E next(){ checkForComodification(); int i = cursor; if (i >= size) thrownew NoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) thrownew ConcurrentModificationException(); cursor = i + 1; return (E) elementData[lastRet = i];}
注意下,这里面有两个非常重要的地方,cursor初始值是0,获取到元素之后,cursor 加1,那么它就是下次索要访问的下标,最后一行,将i赋值给了lastRet这个其实就是上次访问的下标。
此时,cursor变为了1,lastRet变为了0。
最后我们看下ArrayList的remove()方法做了什么?
public boolean remove(Object o) { if (o == null) { for (intindex = 0; index < size; index++) if (elementData[index] == null) { fastRemove(index); return true; } } else { for (intindex = 0; index < size; index++) if (o.equals(elementData[index])) { fastRemove(index); return true; } } return false;}
privatevoidfastRemove(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}
「重点:」
我们先记住这里,modCount初始值是0,删除一个元素之后,modCount自增1,接下来就是删除元素,最后一行将引用置为null是为了方便垃圾回收器进行回收。
到这里,其实一个完整的判断、获取、删除已经走完了,此时我们回忆下各个变量的值:
cursor : 1(获取了一次元素,默认值0自增了1);
lastRet :0(上一个访问元素的下标值);
expectedModCount :0(初始默认值);
modCount : 1(进行了一次remove操作,变成了1);
不知道你还记不记得,next()方法中有两次检查,如果已经忘记的话,建议你往上翻一翻,我们来看下这个判断:
finalvoidcheckForComodification(){ if (modCount != expectedModCount) thrownew ConcurrentModificationException();}
当modCount不等于expectedModCount的时候抛出异常,那么现在我们可以通过上面各变量的值发现,两个变量的值到底是多少,并且知道它们是怎么演变过来的。那么现在我们是不是清楚了ConcurrentModificationException异常产生的愿意呢!
「就是因为,list.remove()导致modCount与expectedModCount的值不一致从而引发的问题。」
我们现在知道引发这个问题,是因为两个变量的值不一致所导致的,那么有没有什么办法可以解决这个问题呢!答案肯定是有的,通过源码可以发现,Iterator里面也提供了remove方法。
publicvoidremove() { if (lastRet < 0) thrownew IllegalStateException(); checkForComodification(); try { ArrayList.this.remove(lastRet); cursor = lastRet; lastRet = -1; expectedModCount = modCount; } catch (IndexOutOfBoundsException ex) { thrownew ConcurrentModificationException(); }}
你看它做了什么,它将modCount的值赋值给了expectedModCount,那么在调用next()进行检查判断的时候势必不会出现问题。
那么以后如果需要remove的话,千万不要使用list.remove()了,而是使用iterator.remove(),这样其实就不会出现异常了。
publicstaticvoidmain(String[] args){ List<Integer> list = new ArrayList<>(); System.out.println("开始添加元素 size:" + list.size()); for (int i = 0; i < 100; i++) { list.add(i + 1); } System.out.println("元素添加结束 size:" + list.size()); Iterator<Integer> iterator = list.iterator(); while (iterator.hasNext()) { Integer next = iterator.next(); if (next % 5 == 0) { iterator.remove(); } } System.out.println("执行结束 size:" + list.size());}
「建议:」
另外告诉大家,我们在进行测试的时候,如果找不到某个类的实现类,因为有时候一个类有超级多的实现类,但是你不知道它到底调用的是哪个,那么你就通过debug的方式进行查找,是很便捷的方法。
其实这个问题很常见,也是很简单,但是我们做技术的就是把握细节,通过追溯它的具体实现,发现它的问题所在,这样你不仅仅知道这样有问题,而且你还知道这个问题具体是如何产生的,那么今后不论对于你平时的工作还是面试都是莫大的帮助。
以上就是小编给大家带来的关于'java遍历list删除元素,java遍历list删除元素讲解'的探讨分享,希望大家通过阅读小编的文章之后能够有所收获!如果大家觉得小编的文章不错的话,可以多多分享给有需要的人。