使用ArrayList进行Java Card Game War

时间:2015-01-18 18:10:46

标签: java arraylist concurrentmodification

如何为获胜者添加被比较的牌。我有并发修改错误。我正在尝试将数组堆添加到获胜者arraylist但我收到错误。

import java.util.ArrayList;

public class Deck {
    ArrayList<Card> deck = new ArrayList<Card>();
    ArrayList<Card> player1 = new ArrayList<Card>();
    ArrayList<Card> player2 = new ArrayList<Card>();
    ArrayList<Card> pile = new ArrayList<Card>();
    boolean winner;

    public Deck(){

    }
    public void createDeck(){
        for(int i =0; i<4; i++){        
            for(int j =1; j<15;j++){            
                if(i==0) deck.add(new Card("Spades", j));                   
                if(i==1) deck.add(new Card("Hearts", j));
                if(i==2) deck.add(new Card("Clubs", j));
                if(i==3) deck.add(new Card("Diamonds",j));

            }
        }
    }
    public void splitDeck(){
        for(int i = 0; i< 52;i++){
            int r = (int) (Math.random()*(51-i));
            if(i%2==0){
                player1.add(deck.get(r));
            }
            else{
                player2.add(deck.get(r));
            }
            deck.remove(r);
        }

    }
    public int [] war(){

        int[]score = {0,0};

        for(Card c : player1){
            int x = player1.indexOf(c);
            if(c.getValue() > player2.get(x).getValue()){
                winner = true;
                score[0]++;
                pile.add(player1.get(x));
                pile.add(player2.get(x));
                distributeCards();
            }
            else if(c.getValue() <player2.get(x).getValue()) score[1]++;
            distributeCards();
        }

        return score;
    }   

    public void distributeCards(){

            if(winner==true){
                for(int i = 0; i<pile.size(); i++){
                    player1.add(pile.get(i));
                }
            }
            else{
                for(int i = 0; i<pile.size(); i++){
                    player2.add(pile.get(i));
                }
            }
        }


    public String warGame(){
        String print= "";
        for(Card c : player1){
            int x = player1.indexOf(c);
            if(c.getValue() > player2.get(x).getValue()){

                print+= "\n"+"Player1 won";
            }
            else if(c.getValue() <player2.get(x).getValue())
                print+="\n"+"Player2 won";

            else if(c.getValue() ==player2.get(x).getValue())
                print+= "\n"+"Its a tie";
        }

        return print;

    }
}

错误讯息是......

Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
    at java.util.ArrayList$Itr.next(Unknown Source)
    at Deck.war(Deck.java:41)
    at War.main(War.java:7)

1 个答案:

答案 0 :(得分:1)

war()内部迭代ArrayList player1,您正在调用distributeCards()来修改player1。您无法在迭代时更改列表 - 这就是您获取ConcurrentModificationException的原因(请阅读docs以获取进一步说明)。

解决方案: 将player1复制到tmp列表 - 并修改该tmp列表,并且只有在您完成遍历player1之后 - 将tmp分配给player1。 (同样的逻辑适用于player2