ConcurrentModificationException

ConcurrentModificationException

本文介绍了为什么没有hasntext,hasprevious等方法的Iterator或listIterator检查ConcurrentModificationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道failfast迭代器检查ConcurrentModificationException尽力而为的基础。为什么没有方法,如hasnext或hasPrevious检查ConcurrentModificationException并抛出它

I know that failfast iterators check for ConcurrentModificationException on best-effort basis.Why doesnt the methods such as hasnext or hasPrevious check for ConcurrentModificationException and throw it???

下面的代码非常好,因为这些方法不检查CME,虽然它已经结构上修改

The following code works well for the precise reason that these methods are not checked for CME although it has been structurally modifed

public static void main(String[] args) {
        // TODO Auto-generated method stub


        List<String> lilong = new ArrayList<String>();
        lilong.add("Queen");
        for(Iterator<String> iter =lilong.iterator();iter.hasNext();)
        {
            System.out.println(iter.next());
            lilong.add("King");
            lilong.remove(0);
            lilong.add("Star");
            lilong.remove(0);
            lilong.add("Sun");
            lilong.remove(0);
            lilong.add("Polar Bear");
            lilong.clear();

            lilong.add("There you go");

        }
        System.out.println("size="+ lilong.size()+"##element##"+lilong.iterator().next());
    }


推荐答案

至于为什么这样实现... +1你的问题。

As for why it is implemented that way... +1 to your question.

这篇关于为什么没有hasntext,hasprevious等方法的Iterator或listIterator检查ConcurrentModificationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 11:12