为什么我的方法没有清空ArrayList <card>?</card>

时间:2014-10-15 04:24:24

标签: java for-loop

我正在尝试清空ArrayList手,以便可以播放新一轮。我不知道为什么我的for循环没有清空“手”而只是删除手中的第一张牌。这是我的代码:

import java.util.ArrayList;

    public class Game {

        private Deck aDeck;
        private InputReader reader;
        private ArrayList<Card> hand;
        private String commandChoice;
        private int handValue;

        /**
         * Method to run the game. 
         * First while loop will run until the player chooses "no" for another round.
         * Second while look will keep running until the player chooses to stand, has 21 or     busts.
         * the last while loop is to make sure that the player chooses either "Hit" or "Stand". If neither is choosen, it will keep requesting it.
         */
        public void Play(){
            int playerPoints = 0;
            int totalRounds = 0;
            commandChoice = "";
            hand = new ArrayList<Card>();
            reader = new InputReader();

            intro();
            aDeck = new Deck();
            aDeck.loadDeck();
            aDeck.shuffle();
            aDeck.shuffle();

            while(anotherRound() == false){
                dealCard();
                dealCard();
                report();

                playTurn();
                totalRounds ++;

                emptyHand();
                endRound();
                anotherRound();

            }
            System.out.println("Player Points: " + playerPoints);
            System.out.println("Total Rounds: " + totalRounds);
        }

        /**
         * intro message to player
         */
        private void intro(){
            System.out.println("Welcome to 1451 Blackjack!");
            System.out.println("You will start with two cards.");
            System.out.println("You will be prompted to 'hit' or 'stand' 'hit' means you want another card, 'stand' not.");
            System.out.println("");
            System.out.println("You are trying to get Blackjack with exactly 21 points.");

        }
        /**
        * deals a card to the player
        */
        private void dealCard(){
        int deckSize = aDeck.deckSize();
        if(deckSize == 0){
            System.out.println("Time for some more cards");
            aDeck.loadDeck();
            aDeck.shuffle();
            aDeck.shuffle();
        } else {
        Card tempCard = aDeck.takeCard();
        hand.add(tempCard);
        }
    }

    /**
     * calculates the hand value of the player
     * @return handValue
     */
    private int getHandValue(){
        handValue = 0;
        for(Card eachCard : hand) {
            int tempValue = eachCard.getValue();
            handValue = handValue + tempValue;
        }
        return handValue;
    }

    /**
     * displays contents of hand
     */
        private void showHand(){
            System.out.println("Your cards:"); 
            for(Card eachCard : hand) {
                    System.out.println(eachCard.getDescription()+ 
                                    " of " + eachCard.getSuit());
             }
        }

        private void emptyHand(){
            for(int count = 0; count < hand.size(); count++) {
                 hand.remove(count);
             }
        }

显然,我的代码在“emptyHand()”方法中破坏了某些东西,但我缺少什么?!让我疯了。

2 个答案:

答案 0 :(得分:1)

问题是由选择删除更改列表大小的索引引起的。除了“不清除”列表之外,这可能还会导致IndexOutOfBounds异常。

考虑{A, B, C, D, E}之手,循环流程如下:通过使用调试器逐步完成循环也可以看到它。

i      size()    result after remove(i)
-----  --------  ----------------------
0      5         {B, C, D, E}
1      4         {B, D, E}
2      3         {B, D}
3      2         -> done  (2 cards left!)

简单的解决方法是使用hand.clear(),尽管其他解决方案包括向后迭代索引或总是“弹出”结束。

for (int i = hand.size() - 1; i >= 0; i--) {
   hand.remove(i);
}

while (hand.size() > 0) {
   hand.remove(hand.size() - 1);
   // or hand.remove(0); but this causes bad complexity bounds on an ArrayList
}

创建一个类似的表格,以证明这些方法是有效的。

答案 1 :(得分:1)

忽略这个问题,我没有意识到ArrayList命令&#34;清除&#34;这正是我试图做的事情。