在java中使用LinkedList创建一副扑克牌



我不知道我在这里做什么,但我正试图用java中的LinkedList集合来创建一副扑克牌。我的逻辑似乎出错了。我一直在甲板构造函数中得到NullPointerException。

我正试图重新编写使用数组来使用LikedList的代码。我错过了什么?

public class Deck {
/** 
 * A LinkedList of cards in the deck, where the top card is the 
 * first index.
 */
private LinkedList<Card> mCards;
/**
 * Number of cards currently in the deck
 */
private int numCards;

/**No args Constructor- if no arguments are used when creating a deck,
 * then we define the game deck to be one deck without shuffling.
 * 
 */
public Deck(){ this(1, false); }
/**Constructor that defines the number of decks (how many sets of 52
 *              cards are in the deck) and whether it should be shuffled.
 * 
 * @param numDecks the number of individual decks in this game deck
 * @param isShuffled whether to shuffle the cards
 */
public Deck(int numDecks, boolean isShuffled){
    this.numCards = numDecks * 52;
    //for each deck
    for (int i = 0; i < numDecks; i++){
        //for each suit
        for(int j = 0; j < 4; j++){
            //for each number
            for(int k = 1; k <= 13; k++){
                //add card to the deck
                this.mCards.add(new Card(Suit.values()[j], k));
            }
        }
    }
    if(isShuffled){
        this.shuffle();
}

public enum Suit {
   Clubs,
   Diamonds,
   Spades,
   Hearts,
}

不初始化mCards

添加(在构造函数的开头):

mCards = new LinkedList<Card>();

相关内容

  • 没有找到相关文章

最新更新