在 java 中,ArrayList 是一个很常用的类,在编程中经常要对 ArrayList 进行增、删、改、查操作。之前在学校时一直认为删除操作是最简单的,现在才越发觉得自己愚蠢。只需要设置好预期条件的查询才是最简单的,删除涉及到存储空间的释放,以及数组的遍历等问题,在list的操作中相对还算小老哥呢。
这两天在给小程序提供后台接口,因为设计的改变,需要对于已查询出来的数组进行遍历删除。在使用 remove 方法对 ArrayList 进行删除操作时,报 java.util.ConcurrentModificationException 异常,下面探讨一下该异常的原因以及解决办法。
1import java.util.ArrayList; 2import java.util.List; 3 4public class Test { 5 6 public static void main(String[] args) { 7 // TODO Auto-generated method stub 8 List<String> listA = new ArrayList<>(); 9 listA.add("a"); 10 listA.add("b"); 11 listA.add("c"); 12 listA.add("d"); 13 listA.add("e"); 14 listA.add("f"); 15 16 for(String str:listA){ 17 if (str == "c") { 18 listA.remove(str); 19 } 20 } 21 } 22}
运行代码发现,在调用 listA.remove(str) 时,会报 java.util.ConcurrentModificationException异常,如下图:
1Exception in thread "main" java.util.ConcurrentModificationException 22 at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901) 33 at java.util.ArrayList$Itr.next(ArrayList.java:851) 44 at com.zhang.Test.main(Test.java:19)
debug 单步发现,在删除倒数第二个元素是,是不会报上面的错误的,除此之外都会报异常。既然发现了规律,那么可以从源码去寻找原因。
我们可以发现 java for循环的实现,是将List 对象托管给迭代器 Iterator,即如果想对List 进行删除操作,均需要经过 Iterator,否则在遍历时会乱掉,故抛出 ConcurrentModificationException 异常。
那么,我们可以看一下 Iterator 迭代器迭代的步骤:
1、判断是否有下个元素:iterator.hasNext()
1public boolean hasNext() { 2 return cursor != size; 3 }
2、获取下个元素并赋值给上面例子中的item变量:item = iterator.next()
1public E next() { 2 checkForComodification(); 3 int i = cursor; 4 if (i >= size) 5 throw new NoSuchElementException(); 6 Object[] elementData = ArrayList.this.elementData; 7 if (i >= elementData.length) 8 throw new ConcurrentModificationException(); 9 cursor = i + 1; 10 return (E) elementData[lastRet = i]; 11 }
经过调试发现,checkForComodification时返回了异常,异常原因为 modCount != expectedModCount:
1final void checkForComodification() { 2 if (modCount != expectedModCount) 3 throw new ConcurrentModificationException(); 4 }
继续跟源码,发现:
a.modCount 是 List 从被 new 新建之后被修改的次数,当 List 调用 remove()等方法时,modCount++;
b.expectedModCount 是 Itreator 期望当前的List被修改的次数;
c.在Iterator初始化的时候将modCount 的值赋给了expectedModCount;
那么,现在就很容易可以知道为什么会报异常了:
1).modCount 会随着调用List.remove方法而自动增减,而expectedModCount则不会变化,就导致modCount != expectedModCount;
2).在删除倒数第二个元素后,cursor=size-1,此时size=size-1,导致 hasNext 方法认为遍历结束;
解决方案:
经过查阅源码可以发现,iterator 也有一个 remove 方法,其中有一个重要的操作为 expectedModCount = modCount 这样就保证了两者的相等。
1public void remove() { 2 if (lastRet < 0) 3 throw new IllegalStateException(); 4 checkForComodification(); 5 6 try { 7 ArrayList.this.remove(lastRet); 8 cursor = lastRet; 9 lastRet = -1; 10 expectedModCount = modCount; 11 } catch (IndexOutOfBoundsException ex) { 12 throw new ConcurrentModificationException(); 13 } 14 }
修改后的代码如下:
1import java.util.ArrayList; 2import java.util.List; 3 4public class Test { 5 6 public static void main(String[] args) { 7 // TODO Auto-generated method stub 8 List<String> listA = new ArrayList<>(); 9 listA.add("a"); 10 listA.add("b"); 11 listA.add("c"); 12 listA.add("d"); 13 listA.add("e"); 14 listA.add("f"); 15 16Iterator<String> it_b=listA.iterator(); 17 18while(it_b.hasNext()){ 19 String a=it_b.next(); 20 if ("c".equals(a)) { 21 it_b.remove(); 22 } 23 } 24 25} }
改完之后,启动 run,运行顺畅无比,借金星小姐姐(or 小哥哥??)的话作为结尾:完美~~
参考: