java中的ConcurrentModificationException

java中的ConcurrentModificationException,java,concurrentmodification,Java,Concurrentmodification,编辑:问题解决了,它确实来自子列表。谢谢 我正在做一个关于遗传算法的项目。 但是我有一个错误,我真的不知道它发生在哪里,所以我需要你的帮助 以下是函数: public synchronized void evolution(){ //TreeSet containing the 2 fathers and children TreeChromosome t = new TreeChromosome(); Chromosome father1 = selection();

编辑:问题解决了,它确实来自子列表。谢谢

我正在做一个关于遗传算法的项目。 但是我有一个错误,我真的不知道它发生在哪里,所以我需要你的帮助

以下是函数:

public synchronized void evolution(){
    //TreeSet containing the 2 fathers and children
    TreeChromosome t = new TreeChromosome();
    Chromosome father1 = selection();
    Chromosome father2;
    do{father2= selection();}
    while(father1==father2);

    t.add(father1);
    t.add(father2);


    Chromosome child1 = OperatorGen.crossRight(father1, father2);
    OperatorGen.swap(father1);

    Chromosome child2 = OperatorGen.crossLeft(father1, father2);        
    OperatorGen.swap(child2);

    t.add(fils1);
    t.add(fils2);

    // we add the best 2 in the population 
       Chromosome best1=t.pollFirst();
       genotype.add(best1);
        Chromosome best2=t.pollFirst();
       genotype.add(best2);

    //we remove the non selected chromosomes
    for (Chromosome chromo : t) {
        if (genotype.contains(chromo) && t.contains(chromo)){
            genotype.remove(chromo);
        }
    }
    genotype.updateRanks();
}
public static Chromosome crossLeft(Chromosome father1, Chromosome father2, int joint){
    List<Ville> listFather1 = father1.getCities();
    List<Ville> listFather2 = pere2.getCities();
    //we copy the first cities of father1
    List<Ville> listChild= listPere1.subList(0, joint);
    City nextCity;
    //we add the cities of father2  
    //block where the error appears, not always at the same line

  for(int i=0;i<listFather2.size();i++){           
        nextCity=listFather2.get(i);

        if(!listChild.contains(nextCity)){
            listChild.add(nextCity);
        }
    }
    Chromosome child= new Chromosome(listChild);
    return child;
 }
当我运行它时,它工作正常,但当我在循环中运行它时,我在OperatorGen.crossLeft上有一个异常。。。 以下是此函数的代码:

public synchronized void evolution(){
    //TreeSet containing the 2 fathers and children
    TreeChromosome t = new TreeChromosome();
    Chromosome father1 = selection();
    Chromosome father2;
    do{father2= selection();}
    while(father1==father2);

    t.add(father1);
    t.add(father2);


    Chromosome child1 = OperatorGen.crossRight(father1, father2);
    OperatorGen.swap(father1);

    Chromosome child2 = OperatorGen.crossLeft(father1, father2);        
    OperatorGen.swap(child2);

    t.add(fils1);
    t.add(fils2);

    // we add the best 2 in the population 
       Chromosome best1=t.pollFirst();
       genotype.add(best1);
        Chromosome best2=t.pollFirst();
       genotype.add(best2);

    //we remove the non selected chromosomes
    for (Chromosome chromo : t) {
        if (genotype.contains(chromo) && t.contains(chromo)){
            genotype.remove(chromo);
        }
    }
    genotype.updateRanks();
}
public static Chromosome crossLeft(Chromosome father1, Chromosome father2, int joint){
    List<Ville> listFather1 = father1.getCities();
    List<Ville> listFather2 = pere2.getCities();
    //we copy the first cities of father1
    List<Ville> listChild= listPere1.subList(0, joint);
    City nextCity;
    //we add the cities of father2  
    //block where the error appears, not always at the same line

  for(int i=0;i<listFather2.size();i++){           
        nextCity=listFather2.get(i);

        if(!listChild.contains(nextCity)){
            listChild.add(nextCity);
        }
    }
    Chromosome child= new Chromosome(listChild);
    return child;
 }
public静态染色体交叉左(染色体父1,染色体父2,int-joint){
List listFather1=father1.getCities();
List listFather2=pere2.getCities();
//我们复制了父亲的第一批城市
List listChild=listPere1.子列表(0,关节);
城市联系;
//我们加上父亲的城市2
//出现错误的块,但不总是在同一行

对于(int i=0;i您不能从当前迭代的集合中删除,除非您使用
迭代器.remove()
方法,这要求您不使用增强的for循环。

考虑到您的代码,您一直在使用相同的城市以及相同的列表。当您使用子列表时,实际上是在旧列表()上创建视图。难怪当您同时处理所有这些列表时,您会得到ConcurrentModificationException

您必须非常小心地处理这些列表(要真正了解哪里出了问题,我们可能需要完整的源代码),或者必须创建新的列表实例而不是子列表


顺便说一句,您翻译大约一半法语的方式使您的代码更容易阅读,而不是更容易阅读。…(pere father,child fils,等等)

您的
基因型
-实例声明在哪里?在包含第一个函数的类中:private static TreeChromosome generic=new TreeChromosome();您必须提供完整的堆栈跟踪。还要指出行号所指的内容。确实如此吗?他正在迭代
t
,但从
genype
中删除,据我所知,这些都是单独的实例。很抱歉=)谢谢你,我会修改它的。我真的不明白为什么在我运行这个函数的时候它会工作,但是在我调用它的时候它不会工作(真的),例如。。。