简体   繁体   English

在嵌套循环中删除项目时发生ConcurrentModificationException

[英]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 ,但它们grupo1grupo2指向相同的现有列表。 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.) (我假设lstPrimeraAgrupacionList<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.

相关问题 从列表中删除项目时出现 ConcurrentModificationException - ConcurrentModificationException when removing item from a list 在Java中使用嵌套的for循环时避免ConcurrentModificationException - Avoiding ConcurrentModificationException when using nested for-loops in Java 如何修复ConcurrentModificationException,从arrayList中删除项目? - How to fix ConcurrentModificationException, removing item from arrayList? 从游戏中删除子弹时出现ConcurrentModificationException - ConcurrentModificationException when removing bullet from game 从队列中添加/删除时获取 ConcurrentModificationException - Getting ConcurrentModificationException when adding/removing from queue 从列表中删除项目 - 没有得到 ConcurrentModificationException - Removing item from a list inside for - didn't get ConcurrentModificationException 在将嵌套哈希图写入文件并在Java中删除元素时发生ConcurrentModificationException错误 - ConcurrentModificationException error while writing nested hashmap to a file and removing element in Java 使用嵌套的迭代器删除没有ConcurrentModificationException的集合项 - Removing collection items without a ConcurrentModificationException using nested Iterators 使用嵌套的 for-each 删除元素时出现 ConcurrentModificationException - ConcurrentModificationException whilst removing element using nested for-each 嵌套的For循环,用于删除数组项 - Nested For Loops for Array Item Removal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM