java – ConcurrentModificationException的幻数
发布时间:2020-12-15 04:22:06 所属栏目:Java 来源:网络整理
导读:我正在使用代码bellow测试集合的ConcurrentModificationException: public static void main(String[] args) { ArrayListString list = new ArrayListString(); list.add("a"); list.add("b"); list.add("c"); for (String s : list) { // if (s.equals("a"
我正在使用代码bellow测试集合的ConcurrentModificationException:
public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); list.add("a"); list.add("b"); list.add("c"); for (String s : list) { // if (s.equals("a")) { // ConcurrentModificationException! if (s.equals("b")) { // -->> Magic number,NO Exception,Why? // if (s.equals("c")) { // ConcurrentModificationException! list.remove(s); } } System.out.println(list); } 我不明白为什么删除“b”可以,但其他人NG? 解决方法
首先要知道的是(如
JLS所述)以下增强的for循环:
for (String s : list) { // Do something with s } 相当于: for (Iterator<String> it = list.iterator(); it.hasNext();) { String s = it.next(); // Do something with s } 如果你看一下AbstractList中迭代器的实现,你会看到: > hasNext()不检查并发修改,只是使用其大小检查我们是否在列表的末尾: public boolean hasNext() { return cursor != size(); } > next()完成的第一件事是调用checkForComodification()以查看在迭代时是否修改了列表: public E next() { checkForComodification(); try { E next = get(cursor); lastRet = cursor++; return next; } catch (IndexOutOfBoundsException e) { checkForComodification(); throw new NoSuchElementException(); } } final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); } 因此,当您迭代并删除列表的倒数第二个元素时,下一条指令将是对hasNext()的调用,它将返回false,因为删除一个元素导致列表的大小减少一个,并且您的迭代将停止而不调用next()并抛出异常. 顺便说一句,所有这些只是一个实现细节,你不应该依赖它,因为它可以改变,并在你迭代时使用 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |