Error in java "java.util.ConcurrentModificationException"

2

Because I reason this exception error when using the iterator in this line:

/*if((itTrayectos.next().getIdEinicial() == idEInicial) && (itTrayectos.next().getIdEfinal() == idEFinal))*/

package Grafos;
import java.io.*;
import java.util.*;

import graphsDSESIUCLM.*;
public class Grafos {
public static void main(String [] args){
    /*
     * - menor o igual que 300 de duracion.
     * - legada y salida a la misma estacion no se considera trayecto.
     * - Trayecto efecturado de origen y destino con las mismas estaciones por diferentes ciudadanos se considera una sola estacion.
     */

    //variables para guaradar   
    int idViaje;
    int duracionSeg;
    int idEInicial;
    int idEFinal;
    double longEInicial;
    double longEFinal;
    double latEInicial;
    double latEFinal;
    //Leer archivo
    String archivo = "MetroBikeShare_2016_Q3_trips.csv";
    Scanner entrada = null;
    String linea = "";
    String separador = ",";

    Graph  gr = new TreeMapGraph<>();
   //Vertex <DecoratedElement> a=gr.insertVertex(v);
  List <Trayecto> datosTrayectos = new <Trayecto> ArrayList();
  Iterator <Trayecto> itTrayectos=datosTrayectos.iterator();



  try {
        entrada = new Scanner(new FileReader(archivo));
        entrada.nextLine();
        boolean trayectoRepe=false;

        while (entrada.hasNextLine()) {
            linea=entrada.nextLine();
            // usa comma como separador
            String[] registro = linea.split(separador);

           idEInicial=Integer.parseInt(registro[4]);
           idEFinal=Integer.parseInt(registro[7]);
           int duracion =Integer.parseInt(registro[1]);
           datosTrayectos.add(new Trayecto());

           if((duracion<=300) && !(registro[5].equals(registro[8]))){
                while(itTrayectos.hasNext()&&(!trayectoRepe)){
                    if((itTrayectos.next().getIdEinicial() == idEInicial) && (itTrayectos.next().getIdEfinal() == idEFinal)){
                        trayectoRepe=true;
                    }

                }
            if(!trayectoRepe){
                datosTrayectos.add(new Trayecto());
            }

         }




       } 

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (!entrada.hasNextLine()) {
            entrada.close();
        }
    }   

}

}

    
asked by gabriel gomez 12.12.2016 в 21:15
source

1 answer

4

This is because you are trying to add elements to your list by which you have created the iterator while you are iterating through it. Through this line:

datosTrayectos.add(new Trayecto());

Therefore, the iterator, upon detecting that the list has been modified, can not guarantee that it iterates through all the data correctly. That's why he is giving you the error, since he can not ensure the integrity of the data.

I recommend that you create another list and use this list to save the new data.

List <Trayecto> nuevosTrayectos = new <Trayecto> ArrayList();

Once all the iterations are finished you can use the method addAll to combine both ArrayList :

datosTrayectos.addAll(nuevosTrayectos);
    
answered by 12.12.2016 / 21:28
source