2012-05-01 23 views
0

我正在通過ArrayList進行搜索並與2個迭代器進行比較。我將值寫出到最終將成爲XML輸出的字符串緩衝區。當我解析值時,我正在檢查匹配的itemIds。比賽通常是零件和圖紙。一部分可以有很多圖紙。對於我的XML,我必須知道所有匹配的類型和名稱並將這些值附加在一起。Java - 解析帶有2個迭代器的ArrayList和可怕的ConcurrentModificationException

使用此ArrayList:

的itemId類型名稱

1000部分錘
1001部分指甲
1000 DWG語義
1002部分尺

我的示例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> 

這是我的第一個環

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 
    } 

我寫了一個方法的第二循環,並比較

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(); 
    } 

當它找到了匹配我想從列表中刪除匹配的條目。沒有必要重新檢查已匹配的條目。所以當第一個循環在列表中繼續時 - 它不必再次檢查該條目。

但我當然得到ConcurrentModificationException。我完全理解爲什麼。 有沒有一種方法,而不是試圖刪除條目,我可以如何標記一個布爾或什麼的,所以當第一個循環到達列表中的條目它知道要跳過它,並轉到下一個?

+1

可以將您的商店的項目,你已經處理的二級結構,如'TreeMap',並檢查那裏呢? – mellamokb

回答

1

將要刪除的所有元素添加到新列表中。

迭代後,調用:

coll1.removeAll (coll2); 

不迭代器,以及它們的hasNext /下一首,但隨着列表,你可以用一個for循環從上到下遍歷。去除元素(7)拜訪訪問元素(6)等對我來說從來都不是問題,但我還沒有看到它被推薦。

這裏完整代碼

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(); 
    } 
} 

輸出:

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 ! 
+0

您可以根據這個建議擴展更多 – jkteater

+0

第一個還是第二個? –

0

最簡單的方法可能是創建另一個列表,在其中放入「匹配」條目,然後檢查該列表。