2012-01-02 2 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

답변

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

나는 지금 그것을 분명히 이해하지 못하고있다. 괜찮으 시다면 다음 번에이 문제가 생기도록 어떻게 할 수 있는지 보여 주시겠습니까? 하지만 2nd for 루프의 문제가 아닌가? – tejas

+0

글쎄, 내가 틀렸다면 오류 자체를 공유하지 않았다. 여전히 두 번째 for 루프는 괜찮 았다고 생각합니다. 단지 값을 쿼리합니다 ... 일부 예제 코드를 포함하여 내 대답을 편집합니다. –

+0

글쎄, 오류는 단지 동시 수정 예외입니다. 다른 건 없어. 나는 심지어 두 번째 루프에 대해서도 의심이된다. 심지어 거기에 값을 업데이 트하기 때문에, promoobj.remove (0); 이 시나리오는 다음과 같이 진행됩니다. promocodeTour의 값이 2이고 루프가 진행될 때, 1로 감소하고 첫 번째 for 루프 이후 조건을 확인합니다. 이제는 하나의 반복에 관해서는 0이되고 이제는 예외를 잡는 중이다. – tejas