简体   繁体   English

使用迭代器时出现 java.util.ConcurrentModificationException 错误?

[英]java.util.ConcurrentModificationException error when using iterator?

Why am I getting this problem when trying to print it out?为什么我在尝试打印时会遇到这个问题? What I'm trying to do is display out the name that was placed in the array.我想要做的是显示放置在数组中的名称。

    ArrayList <Employee> empArray = new ArrayList();
    LinkedList empyrIncm = new LinkedList();
    ListIterator li = empyrIncm.listIterator();
    DecimalFormat df = new DecimalFormat ("#, ##0.00");

for (int i = 0; i<empArray.size(); i++)
    {
        double yrIncm = empArray.get(i).getSalary() * 12;

        if (yrIncm > 80000)
        {
            empyrIncm.add (new String (empArray.get(i).getEmpName()));

            while(li.hasNext())
            {
                System.out.println ("\nName : " + li.next() + "\nTotal yearly income : " + df.format (yrIncm));
            }
        }
        else
        {
            foundyrIncm = false;
        }
    }

From what I know, the iterator I used is to display out the name "one-by-one" without the "comma" when more input are added to the array.据我所知,我使用的迭代器是在向数组中添加更多输入时显示“一一”而不带“逗号”的名称。 Means, without the use of iterator, it will display out by default意思是,不使用迭代器,默认会显示出来

Name : Object1, Object 2, Object 3, Object 4 
Total yearly income : 123

I don't want that comma, instead I want the name to be display out one by one with their yearly income.我不想要那个逗号,而是希望名字和他们的年收入一一显示出来。 Any help would be appreciated.任何帮助,将不胜感激。

You cant do following two line's activity together ieread & write together with regular fail-fast iterator data structures :-您不能将以下两行的活动与常规的快速失败迭代器数据结构一起进行即读和写:-

empyrIncm.add (new String (empArray.get(i).getEmpName()));
while(li.hasNext())

If you want to do both read & write together then do either of the following :-如果您想同时进行读写,请执行以下任一操作:-

empyrIncm.add (new String (empArray.get(i).getEmpName()));
ListIterator li = empyrIncm.listIterator();
while(li.hasNext())

OR或者

Use CopyOnWriteArrayList instead of LinkedList/ArrayList.使用CopyOnWriteArrayList而不是 LinkedList/ArrayList。

Now if you try to print any list then default implementation provided by API prints all element in list by comma seperated, hence the output you getting.现在,如果您尝试打印任何列表,则 API 提供的默认实现会以逗号分隔打印列表中的所有元素,因此您将获得输出。

You are adding element to the list here:您正在此处向列表中添加元素:

empyrIncm.add (new String (empArray.get(i).getEmpName()));

while you are iterating the list here:当您在这里迭代列表时:

System.out.println ("\nName : " + li.next() + "\nTotal yearly income : " + df.format (yrIncm));

The iterator in java are Fail-fast. java中的迭代器是Fail-fast。 It will not allow you to update the list while iterating over it.它不允许您在迭代列表时更新列表。

You need to use CopyOnWriteArrayList instead of LinkedList .您需要使用CopyOnWriteArrayList而不是LinkedList Normally CopyOnWriteArrayList is very expensive because it involves costly Array copy with every write operation.通常CopyOnWriteArrayList非常昂贵,因为每次写入操作都涉及昂贵的 Array 复制。 Use the below statement.使用下面的语句。

CopyOnWriteArrayList empyrIncm = new CopyOnWriteArrayList();

Also your iterator getting call will be postponed until you need it.您的iterator也将被推迟,直到您需要它为止。 Just before the while loop.就在while循环之前。

ListIterator li = empyrIncm.listIterator();

Try initializing the ListIterator just outside the while loop.尝试在 while 循环外初始化ListIterator

ArrayList <Employee> empArray = new ArrayList();
LinkedList empyrIncm = new LinkedList();
DecimalFormat df = new DecimalFormat ("#, ##0.00");

for (int i = 0; i<empArray.size(); i++)
{
    double yrIncm = empArray.get(i).getSalary() * 12;

    if (yrIncm > 80000)
    {
        empyrIncm.add (new String (empArray.get(i).getEmpName()));

        ListIterator li = empyrIncm.listIterator();
        while(li.hasNext())
        {
            System.out.println ("\nName : " + li.next() + "\nTotal yearly income : " + df.format (yrIncm));
        }
    }
    else
    {
        foundyrIncm = false;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用迭代器时出现java.util.ConcurrentModificationException - java.util.ConcurrentModificationException while using iterator 使用Iterator - java.util.ConcurrentModificationException - Using Iterator - java.util.ConcurrentModificationException 带迭代器的java.util.ConcurrentModificationException - java.util.ConcurrentModificationException with iterator java.util.ConcurrentModificationException尝试使用迭代器时出错 - java.util.ConcurrentModificationException Error while trying to use iterator Hibernate java.util.ConcurrentModificationException 中的迭代器错误问题:null - iterator error problem in Hibernate java.util.ConcurrentModificationException: null 错误:java.util.ConcurrentModificationException - Error : java.util.ConcurrentModificationException 在Iterator中获取java.util.ConcurrentModificationException - Getting java.util.ConcurrentModificationException in Iterator 带有迭代器的java.util.ConcurrentModificationException(字符移动) - java.util.ConcurrentModificationException with iterator (character movement) 使用OSC时如何避免java.util.ConcurrentModificationException? - How can java.util.ConcurrentModificationException be avoided when using OSC? Java编程错误:java.util.ConcurrentModificationException - Java Programming Error: java.util.ConcurrentModificationException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM