Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
添加其他对象时的java.util.ConcurrentModificationException_Java - Fatal编程技术网

添加其他对象时的java.util.ConcurrentModificationException

添加其他对象时的java.util.ConcurrentModificationException,java,Java,我为这个例外感到痛苦。我的代码有什么问题? 我只想在另一个ArrayList public class GlennTestMain { static ArrayList<Person> ps; static ArrayList<Person> duplicates; public static void main(String[] args) { ps = new ArrayList<GlennTestMain.P

我为这个例外感到痛苦。我的代码有什么问题? 我只想在另一个
ArrayList

public class GlennTestMain
{

    static ArrayList<Person> ps;

    static ArrayList<Person> duplicates;
    public static void main(String[] args)
    {
        ps = new ArrayList<GlennTestMain.Person>();

        duplicates = new ArrayList<GlennTestMain.Person>();

        noDuplicate(new Person("Glenn", 123));
        noDuplicate(new Person("Glenn", 423));
        noDuplicate(new Person("Joe", 1423)); // error here


        System.out.println(ps.size());
        System.out.println(duplicates.size());
    }

    public static void noDuplicate(Person p1)
    {
        if(ps.size() != 0)
        {
            for(Person p : ps)
            {
                if(p.name.equals(p1.name))
                {
                    duplicates.add(p1);
                }
                else
                {
                    ps.add(p1);
                }
            }
        }
        else
        {
            ps.add(p1);
        }
    }

    static class Person
    {
        public Person(String n, int num)
        {
            this.name = n;
            this.age = num;
        }
        String name;
        int age;
    }



}

您不能修改正在迭代的
集合。这可能会引发
ConcurrentModificationException
。虽然它有时可能有效,但并不保证每次都有效

如果要从列表中添加或删除某些内容,需要对列表使用
迭代器
,或
列表迭代器
。并使用方法在列表中添加任何内容。即使在
迭代器中
,如果尝试使用
List.add
List.remove
,也会出现异常,因为这没有任何区别。您应该使用
迭代器
的方法

请参阅以下文章了解如何使用它:-

根据:

如果线程在使用fail fast迭代器迭代集合时直接修改集合,迭代器将引发此异常

您正在使用迭代添加
Person
对象

您可以进行以下修改:

boolean duplicateFound = false;
for(Person p : ps)
{
    if(p.name.equals(p1.name))
    {
        duplicates.add(p1);
        duplicateFound = true;
    }
}

if( ! duplicateFound)
{
    ps.add(p1);
}
原因? is
返回的迭代器本质上是快速失败的

此类迭代器和
listIterator
方法返回的迭代器是
fail fas
t:如果在创建迭代器后的任何时候,列表在结构上被修改,除了通过迭代器自己的remove或add方法,迭代器将抛出
ConcurrentModificationException
。因此,在面对并发修改时,迭代器会快速、干净地失败,而不是在将来的不确定时间冒着任意、不确定行为的风险

当我不使用迭代器时,迭代器从何而来? 对于集合的增强For循环,
迭代器
被使用,因此在迭代时不能调用
add
方法

因此,您的循环如下所示

for (Iterator<Entry> i = c.iterator(); i.hasNext(); ){   
现在我在哪里可以读更多?

  • +谢谢你的详细回答。它真的帮助了CME的可能重复只有当迭代器被用来迭代集合的元素并修改它们时才会发生吗?
    for (Iterator<Entry> i = c.iterator(); i.hasNext(); ){   
    
        String inputWord = "john";
        ArrayList<String> wordlist = new ArrayList<String>();
        wordlist.add("rambo");
        wordlist.add("john");
        for (ListIterator<String> iterator = wordlist.listIterator(); iterator
                .hasNext();) {
            String z = iterator.next();
            if (z.equals(inputWord)) {
                iterator.add("3");
            }
        }
        System.out.println(wordlist.size());