2012-01-02 22 views
0

有人可以告訴我我要去哪裏嗎?併發修改例外清單

File promoCountFile = new File(RaconTours.PATH + "promocodeCount.txt"); 
      if (promoPlistPath.exists()) { 
       try { 
        ObjectInputStream inStream = new ObjectInputStream(new FileInputStream(promoPlistPath)); 
        ObjectInputStream promoStream = new ObjectInputStream(new FileInputStream(promoCountFile)); 
        promoobj = (ArrayList<HashMap<String, Object>>) promoStream.readObject(); 
        obj = (ArrayList<HashMap<String, Object>>) inStream.readObject(); 
        for (HashMap<String, Object> tmpObj : obj) { 
         promoTourname = (String) tmpObj.get("promoTour"); 
         promocodeID = (String) tmpObj.get("promocode"); 
         if (promoTourname.equals(currentTour.getObjtourName())) { 
          //if the condition is met, remove the entry from the file 
          for (HashMap<String, Object> promoTemp : promoobj) { 
           promoTourCount = (Integer) promoTemp.get("promocodeTourCount"); 
          } 

          obj.remove(tmpObj); 
          --promoTourCount; 

          ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(promoPlistPath)); 
          out.writeObject(obj); 
          out.close(); 

          ObjectOutputStream promoout = new ObjectOutputStream(new FileOutputStream(promoCountFile)); 
          HashMap<String, Object> promoCountDict = new HashMap<String, Object>(); 
          promoobj.remove(0); 
          promoCountDict.put("promocodeTourCount",promoTourCount); 
          promoobj.add(promoCountDict); 
          promoout.writeObject(promoobj); 
          promoout.close(); 


         } 
        } 


        if (obj.size() == 0 || promoTourCount == 0) { 
         promoPlistPath.delete(); 
         promoCountFile.delete(); 
        } 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 

      } 

這裏給出了併發修改異常,而for循環在第二次或之後迭代。

我試圖每次循環迭代時更新文件中的promoTourCount的值。但我沒有做到這一點。由於避免了添加多個對象,我在位置0刪除對象存在並添加新的一到那個位置(promoobj.remove(0);

PLZ幫我

+0

沒有回答我的問題嗎? – tejas 2012-01-02 12:52:43

回答

2

要修改你正在迭代的集合。這會引發錯誤。您遍歷OBJ這裏:

for (HashMap<String, Object> tmpObj : obj) { 

但是從這裏卸下:

obj.remove(tmpObj); 

我建議你存儲的項目在另一個集合中刪除,並從地圖上刪除這些只有當你完成了循環。

編輯:< < 添加示例代碼 >>

List<Integer> toRemove = new LinkedList<Integer>(); 
for (int i = 0; i < obj.size(); i++) { 
    HashMap<String, Object> tmpObj = obj.get(i); 

    if (/* something */) { 
     /* ... */ 
     /* obj.remove(tmpObj); replaced with*/ 
     toRemove.add(0, i); // notice that I add bigger indices first 
    } 
} 
// Here we make the removal from bigger indices to smaller ones 
// Notice that we iterate and remove from different collections. 
for (Integer indexToDelete : toRemove) { 
    obj.remove(indexToDelete); 
} 

這是基本的想法時,要刪除的元素。但是,您需要在for循環中立即輸出obj。那麼可能有一些指數的黑客會爲你做得更好:

for (int i = 0; i < obj.size(); i++) { 
    HashMap<String, Object> tmpObj = obj.get(i); 

    if (/* something */) { 
     /* ... */ 
     /* obj.remove(tmpObj); replaced with*/ 
     obj.remove(i); // We erase the element but this time we do not use enhanced for loop which is ok. 
     i--; // we decrease the index because th enumber of elements decreased. Index hack. Ugly. 
     System.out.println(obj); // modified obj :) 
    } 
} 
+0

我不清楚,現在學習列表。如果你不介意,你能告訴我怎麼做,以便我下次贏得這個問題嗎?但是,這不是第二個循環的問題嗎? – tejas 2012-01-02 12:57:54

+0

如果我沒有錯,那麼你自己並沒有分享這個錯誤,使用它我會更容易確定。儘管如此,我認爲第二個for循環是好的,它只是查詢值...我將編輯我的答案,包括一些示例代碼。 – 2012-01-02 13:10:59

+0

那麼,錯誤只是併發修改異常。沒有其他的。我懷疑即使第二個循環也應該有問題。因爲即使在那裏我正在更新值,promoobj.remove(0);這種情況是這樣的,比如說我最初對promocodeTour的值爲2,並且循環結束,它會遞減到1,並在第一次for循環之後檢查條件是否爲條件。現在,當它進行一次迭代時,它變爲0,現在它捕捉異常而不是來如果 – tejas 2012-01-02 13:21:24