[英]ConcurrentModificationException with Iterators
我一直在编写一个程序,我遇到了一些我能够正确解决的问题。 但是,我的程序抛出一个ConcurrentModificationException
,我不知道我能做什么。
我正在使用NetBeans 8.0.2编写代码来创建Java桌面应用程序(根据我的教授的要求)。 该计划的目的是管理一家酒店。 因此,它有一些部分,如“客户”,“员工”和“预订”(给我带来问题的那个)。
在每个部分,我都有一个使用DefaultTableModel
的JTable
。 在每个部分中,我都在使用硬盘驱动器上的文件来使更改持久化。 我编写了新的客户/员工课程。 现在,我正在尝试编码预订和取消预订方法。 在我进入“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
的内容已经更改,那么如果您尝试使用或重用它,则会出现异常。 创建一个新的 - 作为一般规则, 不要重用迭代器 。
你是:
您应该在创建迭代器之前修改列表。
此外,您应该尝试最小化局部变量的范围 。
相反:
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();
}
一些不相关的观点:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.