简体   繁体   English

Java-解析具有2个迭代器和可怕的ConcurrentModificationException的ArrayList

[英]Java - Parsing ArrayList with 2 iterators and the dreaded ConcurrentModificationException

I am searching through a ArrayList and doing a compare with 2 iterators. 我正在搜索ArrayList并与2个迭代器进行比较。 I am writing out values to a String buffer that will eventually be a XML output. 我正在将值写到String缓冲区,该缓冲区最终将是XML输出。 As I parse through the values I am checking for matching itemIds. 当我解析这些值时,我正在检查匹配的itemIds。 The matches are normally parts and drawings. 火柴通常是零件和图纸。 There can be many drawings to a part. 一个零件可以有很多图纸。 For my XML I have to know the type and name of all the matches and append the values together. 对于我的XML,我必须知道所有匹配项的类型和名称,并将值附加在一起。

Using this ArrayList: 使用此ArrayList:

itemId type name itemId类型名称

1000 part hammer 1000零件锤
1001 part nail 1001部分指甲
1000 dwg semantic 1000 dwg语义
1002 part ruler 1002尺子

My sample XML output would roughly look like : 我的样本XML输出大致如下所示:

<Master itemId=1000 type=part name=hammer>
  <Extra type=dwg name=semantic>
</Master>
<Master itemId=1001 type=part name=nail>
</Master>
<Master itemId=1002 type=part name=ruler>
</Master>

This is my first loop 这是我的第一个循环

while (theBaseInterator.hasNext()){
     ImportedTableObjects next = theBaseInterator.next(); 
     currentEntry = next.identiferId;
     currentType = next.typeId;
     currentDatasetName = next.nameId;
     compareInterator = tArray.listIterator(theBaseInterator.nextIndex());
     compareEntriesofArray(currentEntry, currentType, currentDatasetName, compareInterator); <======= calling method for 2nd loop and compare check
  }

I written a method for the second loop and compare 我为第二个循环编写了一个方法并进行比较

private void compareEntriesofArray(Object currentEntry, Object currentType, Object currentDatasetName, ListIterator<ImportedTableObjects> compareInterator)
object nextEntry;   
while (compareInterator.hasNext()) {
    ImportedTableObjects next = compareInterator.next();
    nextEntry = next.identiferId;
    if(nextEntry.equals(currentEntry)) { 
    compareInterator.remove();
    }   

When it finds a match I am trying to remove the matching entry from the list. 当找到匹配项时,我正尝试从列表中删除匹配项。 There is no need to re-check entries that has been matched. 无需重新检查已匹配的条目。 So when the 1st loop continues down the list - it does not have to check that entry again. 因此,当第一个循环继续在列表中向下移动时-不必再次检查该条目。

But of course I am getting the ConcurrentModificationException. 但是当然我得到了ConcurrentModificationException。 I fully understand why. 我完全理解为什么。 Is there a way that instead of trying to remove the entry, I can some how mark it with a boolean or something, so when the first loop gets to that entry in the list it knows to skip it and go to the next? 有没有一种方法可以代替尝试删除该条目,而是可以用布尔值或其他方式标记它,因此当第一个循环到达列表中的该条目时,它知道将其跳过并转到下一个?

Add all elements you want to remove into a new List. 将要删除的所有元素添加到新列表中。

After iterating, call: 迭代后,调用:

coll1.removeAll (coll2);

Not with iterators, and their hasNext/next, but with Lists, you can iterate with a for-loop from top to bottom. 不是使用迭代器及其hasNext / next,而是使用列表,您可以从上到下使用for循环进行迭代。 removing element (7) bevore visiting element (6) and so on has never been a problem for me, but I haven't seen it being recommended. 删除元素(7)之前访问元素(6)等等对我来说从来都不是问题,但我还没有看到有人推荐它。

Here complete code 这里完整的代码

import java.util.*;

public class GuessGame 
{
    public static void main ( String [] args )
    {
        char [] ca = "This is a test!".toCharArray ();
        List <Character> ls = new ArrayList <Character> ();
        for (char c: ca)
            ls.add (c);

        show (ls);
        // first method: remove from top/end and step backwise:
        for (int i = ls.size () - 1; i >= 0; --i)
        {
            char c = ls.get (i); 
            if (c == 'i' || c == 'a' || c == 'e')
                ls.remove (i); 
        }
        show (ls);

        // second approach: collect elements to remove ...
        ls = new ArrayList <Character> ();
        for (char c: ca)
            ls.add (c);
        show (ls);
        // ... in a separate list and 
        List <Character> toRemove = new ArrayList <Character> ();
        for (char c: ls)
        {
            if (c == 'i' || c == 'a' || c == 'e')
                toRemove.add (c); 
        }
        // ... remove them all in one go:
        ls.removeAll (toRemove);
        show (ls);
    }

    private static void show (List <Character> ls)
    {
        for (char c: ls)
            System.out.print (c + " ");
        System.out.println ();
    }   
}

Output: 输出:

T h i s   i s   a   t e s t ! 
T h s   s     t s t ! 
T h i s   i s   a   t e s t ! 
T h s   s     t s t ! 

最简单的方法可能是创建另一个列表,在该列表中放置“匹配的”条目,然后对照该列表进行检查。

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

相关问题 java.util.ConcurrentModificationException 从 arraylist 中删除元素时,即使使用迭代器 - java.util.ConcurrentModificationException when removing elements from arraylist even with iterators java ArrayList中的ConcurrentModificationException - ConcurrentModificationException in java ArrayList 使用2个迭代器的java.util.ConcurrentModificationException - java.util.ConcurrentModificationException using 2 iterators Java-具有递归方法和嵌套迭代器的ConcurrentModificationException - Java - ConcurrentModificationException with recursive method and nested iterators 与迭代器的ConcurrentModificationException - ConcurrentModificationException with Iterators Java-同步的ArrayList仍然ConcurrentModificationException - Java - synchronized ArrayList still ConcurrentModificationException ArrayList删除时出现Java ConcurrentModificationException - Java ConcurrentModificationException when on ArrayList Deletion ConcurrentModificationException-checkForComodification(ArrayList.java) - ConcurrentModificationException - checkForComodification(ArrayList.java) ArrayList上的java.util.ConcurrentModificationException - java.util.ConcurrentModificationException on ArrayList java.util.ConcurrentModificationException - ArrayList - java.util.ConcurrentModificationException - ArrayList
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM