繁体   English   中英

与迭代器的ConcurrentModificationException

[英]ConcurrentModificationException with Iterators

我一直在编写一个程序,我遇到了一些我能够正确解决的问题。 但是,我的程序抛出一个ConcurrentModificationException ,我不知道我能做什么。

我正在使用NetBeans 8.0.2编写代码来创建Java桌面应用程序(根据我的教授的要求)。 该计划的目的是管理一家酒店。 因此,它有一些部分,如“客户”,“员工”和“预订”(给我带来问题的那个)。

在每个部分,我都有一个使用DefaultTableModelJTable 在每个部分中,我都在使用硬盘驱动器上的文件来使更改持久化。 我编写了新的客户/员工课程。 现在,我正在尝试编码预订和取消预订方法。 在我进入“unbooking”部分之前,一切都很顺利。

在表和模型之后我有一个ArrayList 在这个特定的部分,我有三个:

  • 整个桌子一个
  • 一个只包含免费房间
  • 一个只有已预订的房间。

预订部分很好,我能够修改表,文件和ArrayList没有任何错误,我的程序实际上做了应有的事情。 但我无法让unbooking部分工作。 它基本上应该与预订相反。

我将发布我的一小部分代码,但如果您需要了解其他任何内容或需要我的代码的更多部分,我很乐意分享它。

我的代码:

public class GestionInstalaciones extends javax.swing.JFrame {
    private final String ruta = System.getProperties().getProperty("user.dir");
    private final File archivo = new File (ruta+"\\Instalaciones.txt");
    private final DefaultTableModel modelo = new DefaultTableModel();
    private final ArrayList contenidoInstalaciones;
    private final ArrayList contenidoInstalacionesOcupadas;
    private final ArrayList contenidoInstalacionesLibres;
    public GestionInstalaciones() {
       initComponents ();
       contenidoInstalaciones = new ArrayList();
       contenidoInstalacionesOcupadas = new ArrayList();
       contenidoInstalacionesLibres = new ArrayList();
       //Añadimos las columnas a la tabla.
       modelo.addColumn ("Tipo");
       modelo.addColumn ("Nombre Instalacion");
       modelo.addColumn ("NIF del Ocupante");
       cargarTabla();
}

private void cargarTabla(){
    this.contenidoInstalaciones.clear();
    FileReader fr = null;
    BufferedReader br;
    String tipo;
    String nombre;
    String NIFocupante;
    String[] partes;
    String linea;

    try{
        fr = new FileReader(archivo);
        br = new BufferedReader(fr);

        while ((linea=br.readLine())!=null) {
            //Adding info to general ArrayList
            this.contenidoInstalaciones.add(linea);
            //Splitting line into 3 components.
            partes = linea.split(",",3);
            tipo = partes[0];
            nombre = partes[1];
            NIFocupante = partes[2];

            //Skipping header.
            if ( tipo.equals( "Tipo" )) { continue; }

            //Añadimos la fila al modelo.
            modelo.addRow(partes);
        }
        TablaInstalaciones.setModel(modelo);
    }
    //Capturamos excepciones y cerramos fichero.
    catch(IOException e) {}
    finally { try { if ( null != fr ) { fr.close(); } } catch (IOException e2){ } }
}//end cargarTabla()

private void botonLiberarInstalacionActionPerformed(java.awt.event.ActionEvent evt) {                                                        
    Object linea;
    int contador=0;
    String aux; 
    String tiposATrabajar = "";
    String[] tiposAInsertar;
    Iterator instalacionesOcupadas;

    //Cleaning of already booked ArrayList.
    //this.contenidoInstalacionesOcupadas.clear();

    instalacionesOcupadas = contenidoInstalacionesOcupadas.iterator();
    this.comboTipoALiberar.removeAllItems();
    this.comboTipoALiberar.addItem("Seleccione");

    //Reading the general Table.
    for (int z = 0; z < TablaInstalaciones.getRowCount() ; z++) {
        //The booking parameter is on the 3rd place.
        if(!TablaInstalaciones.getValueAt(z,2).equals("")){
            //Putting the line into the ArrayList for booked rooms..
            linea = TablaInstalaciones.getValueAt(z,0) + "," + TablaInstalaciones.getValueAt(z,1) + "," + TablaInstalaciones.getValueAt(z,2);
            this.contenidoInstalacionesOcupadas.add(linea);
            contador++;
        }
    }

    **//Reading the booked ArrayList to put the right values on the combobox related. 
      //===> THIS LINE IS GIVING THE ERROR !!!
    while(instalacionesOcupadas.hasNext()) {** 
        aux = instalacionesOcupadas.next().toString().split(",",3)[0];
        //Checking to add only 1 kind of each room type.
        if(!tiposATrabajar.contains(aux)); {
            if (tiposATrabajar.equals("")) { tiposATrabajar=aux; }
            else { tiposATrabajar = tiposATrabajar + "," + aux; }
        }
    }      
    //
    tiposAInsertar = tiposATrabajar.split(",");
    //Adding the type into the combobox.
    for (String elemento: tiposAInsertar){ this.comboTipoALiberar.addItem(elemento.replace(",","")); }

}   

当您正在循环遍历所述链时,对元素链(列表)进行更改时会发生ConcurrentModificationException

我经常遇到它并且总是通过反向循环遍历列表来解决它,这样你就可以向后循环,如果你想要的话你可以改变或删除元素而不会搞乱你循环的链(列表)。

我会为你提供代码,但我觉得很难读你的代码,所以现在必须这样做。

希望这有帮助,祝你好运!

如果自上次使用迭代器以来,您正在迭代的Collection的内容已经更改,那么如果您尝试使用或重用它,则会出现异常。 创建一个新的 - 作为一般规则, 不要重用迭代器

你是:

  1. 创建一个迭代器
  2. 修改列表
  3. 列表修改后使用迭代器←错误!

您应该在创建迭代器之前修改列表。

此外,您应该尝试最小化局部变量的范围

相反:

Iterator<String> someIterator = strings.iterator();
while (someIterator.hasNext()) {
    doSomething();
}

你应该这样做:

for (Iterator<String> iter = strings.iterator(); iter.hasNext();) {
    doSomething();
}

当然,如果您(如您所说) 不需要修改列表,请使用for-each循环:

for (String s : strings) {
    doSomething();
}

一些不相​​关的观点:

  • 为什么要编写extends java.awt.JFrame类的东西? 导入它并改为使用JFrame
  • 在需要时声明并初始化变量。 如果你for循环前初始化了Iterator ,你就不会遇到这个问题。
  • 使用泛型 非泛型Collection只会在运行时出错,而泛型Collection则会产生编译错误。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM