arraylist java.util.ConcurrentModificationException从其他类初始化

时间:2016-03-01 03:10:33

标签: java arraylist

我得到以下异常堆栈跟踪:

Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
    at java.util.ArrayList$Itr.next(ArrayList.java:851)     at
    Controllers.Manager.createTheNetwork(Manager.java:123)  at
    Controllers.Main.main(Main.java:53)

为什么我会得到这个堆栈跟踪?我刚刚从另一个班级初始化了ArrayList

Main.java

public class Main {

    private static Manager  anager;
    private static ArrayList<Vfoo> aVfooList;
    private static ArrayList<Pfoo> aPfooList;
    private static ArrayList<Nfoo> aNfooList;

    public static void main(String[] args) {

        aPfooList = new ArrayList<Pfoo>();
        aVfooList = new ArrayList<Vfoo>();
        aNfooList = new ArrayList<Nfoo>();

        aPfooList.add(new Pfoo());
        aPfooList.add(new Pfoo());

        for (Pfoo p : aPfooList)
            if (!p.isAMethod())
                aVfooList.add(new Cfoo());

        aNfooList.add(new Nfoo());
        aNfooList.add(new Nfoo());

        manager = new Manager();
        manager.setList(aList1);
        manager.setList(aList2);

        manager.createSomething();
}
}

Manager.java

import java.util.ArrayList;

public class Manager {

    private ArrayList<Nfoo> nfooList = new ArrayList<Nfoo>();   
    private ArrayList<Vfoo> vfooList = new ArrayList<Vfoo>();   


    public NodeManager (){

    }


    public ArrayList<Nfoo> getNfooList(){
        return nfooList;
    }


    public void addNfooToTheNfooNetwork(Nfoo n){
        this.nfooList.add(n);
        System.out.println(n);
    }

    public void addANewVfooToTheNetwork(Vfoo aVfoo){
        getNfooList().get(0).addToStack(aVfoo);
    }


    public void addANewTfooToTheNetwork(Tfoo t, int whichNfoo){
        getNfooList().get(whichNfoo).addToStack(t);
    }

    public void resetNfooNetwork(){
        nfooList.clear();
    }

    public void createSomething(){
        for (Nfoo n : nfooList)
            addNfooToTheNfooNetwork(n);

        for (Vfoo v : vfooList)
            addANewVfooToTheNetwork(v); 
    }


    public void createSomething(ArrayList<Nfoo> nl, ArrayList<Vfoo> vl){

        this.setNfooList(nl);
        this.setVfooList(vl);

        for (Nfoo n : nfooList)
            addNfooToTheNfooNetwork(n);

        for (Vfoo v : vfooList)
            addANewVfooToTheNetwork(v); 
    }

    public void setNfooList(ArrayList<Nfoo> nl){
        this.nfooList = nl;
    }

    public void setVfooList(ArrayList<Vfoo> vl){
        this.vfooList = vl;
    }

}

1 个答案:

答案 0 :(得分:0)

确实,正如您在评论中所写,您正在使用迭代器。请查看for循环的文档:Nfoo Objects

正如@Nagesh Susaria在评论中所说,你的实际错误是你正在尝试重新初始化或者只是在你的ArrayList中添加新的Iterator,而来自循环的 for (Nfoo n : nfooList) addNfooToTheNfooNetwork(n); 正在解析它。 请检查以下内容:

public void addNfooToTheNfooNetwork(Nfoo n){
    this.nfooList.add(n);
    System.out.println(n);
}

this.nfooList.add(n);

正如您将看到的那样,&#34;故障&#34;您的代码位于addNfooToTheNfooNetwork(n); dexOptions { preDexLibraries = true jumboMode = true javaMaxHeapSize "2g" //doesn't seem to be supported with multi dex // incremental true } 。 我猜他们两个都在做同样的事情。删除其中一个,你没事!