2014-05-23 24 views
0

我的主要問題是「ConcurrentModificationException」。 我想刪除一行,找到它。但刪除行後我的列表不會更新。所以我得到了這個缺陷。我不知道如何解決它。我看已經在這裏,谷歌,一些書,但我不知道如何與列表中的一個對象[]解決這....這得多,我ConcurrentModificationException(列表<object []>)或其他數據結構「搜索/比較和過濾」

或者是它更好地使用另一個數據structur爲排序和搜索,如果是的話,哪一個會好?(列表對象[]中有很多數據)我怎麼能將它轉換爲該數據結構?

初學者問題

對不起...... 感謝您的幫助解答!

List<Object[]> allIds是一個參數;

  for (Object[] privateIds : allIDs) { 


     for (Object[] comparePrivateIdS : allIds) { 

      if (privateIds[1].equals(comparePrivateIdS[1]) && privateIds[2].equals(comparePrivateIdS[2])) { 
       System.out.print("ok"); 

       int index = allIds.indexOf(comparePrivateIdS); 
       allIds.remove(comparePrivateIdS); 

      } else { 
       System.out.println("Do Nothing"); 
      } 
     } 
+0

見http://stackoverflow.com/questions/1675037/removing-items-from-a-collection-in-java-while-iterating -超過它 – DirkyJerky

回答

1

您可能不叫allIds.remove(...)在遍歷​​,這將引發的ConcurrentModificationException。相反,你必須使用一個明確的迭代器,並調用其remove方法:

for (Iterator<Object[]> it = allIds.iterator(); it.hasNext();) { 
    Object[] comparePrivateIdS = it.next(); 
    //... 
    if(...) it.remove(); 
}