[英]ConcurrentModificationException when removing item in nested loops
This is my code, and I obtained a ConcurrentModificationException
when I removed an element. 这是我的代码,删除元素后获得了
ConcurrentModificationException
。 I do not understand why itrGrupo1
is affected by removing an element in itrGrupo2
. 我不明白为什么
itrGrupo1
被移除元素的影响itrGrupo2
。
The exception occurs at: Instancia inst1=(Instancia) itrGrupo1.next()
when the second while is finished. 第二个时间段结束时,发生以下情况:
Instancia inst1=(Instancia) itrGrupo1.next()
。
for (int i=0; i<lstPrimeraAgrupacion.size();i++){
List grupo1=new ArrayList();
List grupo2=new ArrayList();
grupo1=(List) lstPrimeraAgrupacion.get(i);
grupo2=(List) lstPrimeraAgrupacion.get(i);
Iterator itrGrupo1 = grupo1.iterator();
while(itrGrupo1.hasNext()) {
List nuevoGrupo=new ArrayList();
Instancia inst1=(Instancia) itrGrupo1.next();
int edad1=Integer.valueOf(inst1.getEdadH());
int carnet1=Integer.valueOf(inst1.getCarnetH());
int antigCli1=Integer.valueOf(inst1.getAntigCli());
Iterator itrGrupo2 = grupo2.iterator();
while(itrGrupo2.hasNext()) {
Instancia inst2=(Instancia) itrGrupo2.next();
int edad2=Integer.valueOf(inst2.getEdadH());
int carnet2=Integer.valueOf(inst2.getCarnetH());
int antigCli2=Integer.valueOf(inst2.getAntigCli());
if(cond){
nuevoGrupo.add(inst2);
itrGrupo2.remove();
}
}
// Put in the final list
if (!nuevoGrupo.isEmpty()){
lstNuevosGrupos.add(nuevoGrupo);
}
}
}
The problem exists due to the following lines: 存在此问题是由于以下几行:
List grupo1=new ArrayList();
List grupo2=new ArrayList();
grupo1=(List) lstPrimeraAgrupacion.get(i);
grupo2=(List) lstPrimeraAgrupacion.get(i);
The first two lines of this create two new ArrayList
s, but the lines following them make grupo1
and grupo2
point to the same, existing list. 此命令的前两行创建两个新的
ArrayList
,但它们grupo1
和grupo2
指向相同的现有列表。 I feel that this is desired for grupo2
, since you are modifying it, and it looks like the desired effect of this code is to modify lstPrimeraAgrupacion
's i
th sub-list. 我认为这对于
grupo2
是理想的,因为您正在对其进行修改 ,并且看来此代码的理想效果是修改了lstPrimeraAgrupacion
的第i
个子列表。 (I'm assuming that lstPrimeraAgrupacion
is a List<List<Instancia>>
, or a list of sub-lists of Instancia
objects.) (我假设
lstPrimeraAgrupacion
是List<List<Instancia>>
或Instancia
对象的子列表的列表。)
What the body of your code does is look through grupo1
for any elements that match a condition, and then removes them from grupo2
. 您的代码主体所做的是通过
grupo1
查找与条件匹配的任何元素,然后将其从grupo2
删除。 However, if these two variables reference the same List<Instancia>
object, then modifying grupo2
will also change what your iterator over grupo1
has access to, potentially leaving it in a broken state (which is why you got the exception). 但是,如果这两个变量引用相同的
List<Instancia>
对象,则修改grupo2
还将更改您在grupo1
上的迭代器可以访问的内容,从而可能使其处于损坏状态(这就是为什么会出现异常)。
Now, there are far better approaches to resolving the problem of iterating over a list and removing elements, but the way that you've designed your code, the lowest-impact solution would be to simply duplicate the elements into a new List
, and use that as a snapshot of the old list, while you remove elements as needed from the actual list. 现在,有更好的方法来解决遍历列表和删除元素的问题,但是,在设计代码的方式中,影响最小的解决方案是将元素简单地复制到新的
List
中并使用这是旧列表的快照,同时您可以根据需要从实际列表中删除元素。 To do this, you can change the above code to the following: 为此,您可以将上面的代码更改为以下代码:
List grupo1=new ArrayList();
List grupo2=null;
grupo1.addAll((List) lstPrimeraAgrupacion.get(i)); // Copies the list into our new ArrayList pointed to by grupo1
grupo2=(List) lstPrimeraAgrupacion.get(i); // Makes grupo2 point to the existing List so we can modify it directly.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.