2012-03-16 38 views
0

在這個程序中刪除元素我一直認爲殺人最好的決鬥決鬥者的名單時,他們隨機INT = 0 我有我的底部附近循環的麻煩和正在此錯誤:從一個ArrayList

Exception in thread "main" java.util.ConcurrentModificationException 
at java.util.AbstractList$Itr.checkForComodification(Unknown Source) 
at java.util.AbstractList$Itr.next(Unknown Source) 
at Duelist.main(Duelist.java:74) 

代碼

import java.util.Arrays; 
import java.util.Random; 
import java.util.ArrayList; 
import java.util.Collections; 

public class Duelist implements Comparable{ 

private String name; 
private int chance; 
private boolean alive; 


public Duelist(String duelistName, int hitChance){ 
    alive = true; 
    name = duelistName; 
    chance = hitChance; 

    } 
public String getName(){ 
    return name; 
} 
public int getChance(){ 
    return chance; 
} 
public boolean getLife(){ 
    return alive; 
} 
public void kill(){ 
    alive = false; 
} 

public int compareTo(Object anotherDuelist) throws ClassCastException { 
     if (!(anotherDuelist instanceof Duelist)) 
      throw new ClassCastException("A Duelist object expected."); 
     int anotherDuelistChance = ((Duelist) anotherDuelist).getChance(); 
     return this.chance - anotherDuelistChance;  
     } 

public static void main(String[] args){ 

ArrayList<Duelist> duelers = new ArrayList<Duelist>(5); 
//ArrayList<Duelist> personToKill= new ArrayList<Duelist>(5); 
ArrayList<Duelist> rank = new ArrayList<Duelist>(5); 
Random generator = new Random(); 




Duelist Antoine = new Duelist("Antoine", 3); 
Duelist Francis = new Duelist("Francis", 6); 
Duelist Juliette = new Duelist("Juliettee", 1); 

duelers.add(Antoine); duelers.add(Francis); duelers.add(Juliette); 



//System.out.println(duelers); 


//for(Duelist element : duelers){ 
// System.out.println(element.getName()); 
// System.out.println(element.getChance()); 
//} 
Collections.sort(duelers); 
Collections.reverse(duelers); 
//for(Duelist element : duelers){ 
    //System.out.println(element.getName()); 
    //System.out.println(element.getChance()); 
//} 

while(duelers.size() > 1){ 


    for(Duelist element : duelers){ 



      System.out.println(element.getName()); 
      System.out.println("Chance:" + element.getChance()); 
      int randomInt = generator.nextInt(element.getChance()); 
      System.out.println("Random int " + randomInt); 



      //Destroy target if randomInt equals 0 
      if (randomInt == 0){ 


       //Check to make sure the best duelist is not killing themselves 
       if (element.equals(duelers.get(duelers.size()-1))){ 
        System.out.println("LASTDUDE"); 
        Duelist bestDueler = duelers.get(duelers.size()-2); 
        bestDueler.kill(); 
        rank.add(element); 
        duelers.remove(bestDueler); 
       } 

       else { 
        System.out.println("Killshot"); 
        Duelist bestDueler = duelers.get(duelers.size()-1); 
        bestDueler.kill(); 
        rank.add(element); 
        duelers.remove(bestDueler); 
       } 

      } 


    } 
} 
System.out.println(duelers); 


} 
} 
+3

請發佈一個*更短*的示例,沒有所有註釋掉的行,沒有大量帶有多個空白行的區域,並帶有正確的縮進。我敢肯定,你可以想出一個例子,它只需要很少的空間,它甚至不需要在SO中查看時滾動... – 2012-03-16 21:06:18

+0

[ArrayList的ConcurrentModificationException]的可能重複(http://stackoverflow.com/questions/3184883/concurrentmodificationexception-for-arraylist) – CoolBeans 2012-03-16 21:08:17

回答

3
for(Duelist element : duelers){ 

雖然你是這個塊內,你遍歷duelers列表,這意味着你不能沒有造成改變列表除非您使用迭代器的remove()方法。

這裏的問題是,你沒有訪問迭代器。所以你必須給你的foreach循環使用迭代器變成一個while循環:

Iterator<Duelist> it = duelists.iterator(); 
while(it.hasNext()){ 
    Duelist element = it.next(); 
    // continue as before 

(評論後更新) 或更好:

for(Iterator<X> it=x.iterator();it.hasNext();){ 
    Duelist element = it.next(); 
    //continue as before 

,而不是和

duelers.remove(element); 

編寫

// the problem here is: you can only remove the current element, 
// so you'll have to re-think some of your code 
it.remove(); 

或者:您可以爲迭代創建列表的副本。這是浪費內存(和代碼味道),但如果你的應用程序很小,它應該沒有什麼區別,並且它將需要最少量的重寫。

for(Duelist element : new ArrayList<Duelist>(duelers)){ 
// continue as above 
+1

您應該對Iterator使用for循環來限制Iterator的範圍。 – 2012-03-16 21:27:08

+0

@Steve true,但我討厭'for(Iterator it = x.iterator(); it.hasNext();)'循環最後一段爲空的地方。但是我認爲這是更好的封裝 – 2012-03-16 21:30:10

0

問題是此行duelers.remove(bestDueler);

你不應該在你遍歷它來修改列表,而不是由for(Duelist element : duelers)
使用迭代器使用
您隱含使用迭代器迭代器明確地說即duelers.listIterator()並使用removeiterator而不是

1

您不能修改foreach循環中的集合,而是使用Itera如果您想刪除某些內容,請使用Iterator.remove():

相關問題