ArrayList 是空的,但是为什么呢?

发布于 2024-10-25 05:55:22 字数 1279 浏览 3 评论 0原文

我正在尝试将卡片添加到 ArrayList 甲板上,但它似乎不起作用(大部分代码是 oracle.com 上的示例)。我可能正在做一些非常愚蠢的事情,但我似乎找不到它..这是代码:

public class Card {

    public enum Rank 
    {
        DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE,
        TEN, JACK, QUEEN, KING, ACE
    }

    public enum Suit
    {
        HEARTS, DIAMONDS, SPADES, CLUBS
    }

    private final Rank rank;
    private final Suit suit;
    private static final List<Card> deck = new ArrayList<Card>();

    public Card(Rank rank, Suit suit)
    {
        this.suit = suit;
        this.rank = rank;
    }
    // initializes deck
    public void initDeck()
    {
        for (Suit suit : Suit.values())
        {
            for (Rank rank : Rank.values())
            {
                deck.add(new Card(rank, suit));
            }
        }
    }
    // returns a copy of the deck    
    public static ArrayList<Card> newDeck()
    {
        return new ArrayList<Card>(deck);
    }

    public Rank getRank()
    {
        return rank;
    }

    public Suit getSuit()
    {
        return suit;
    }

    public String toString()
    {
        return rank +" of "+ suit;
    }

    public static void main(String[] args)
    {
        System.out.println(deck.toString());
    }

}

I'm trying to add Cards to ArrayList deck, but it doesn't seem to work(most of the code is an example on oracle.com ). I'm probably doing something really stupid, but I can't seem to find it.. this is the code:

public class Card {

    public enum Rank 
    {
        DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE,
        TEN, JACK, QUEEN, KING, ACE
    }

    public enum Suit
    {
        HEARTS, DIAMONDS, SPADES, CLUBS
    }

    private final Rank rank;
    private final Suit suit;
    private static final List<Card> deck = new ArrayList<Card>();

    public Card(Rank rank, Suit suit)
    {
        this.suit = suit;
        this.rank = rank;
    }
    // initializes deck
    public void initDeck()
    {
        for (Suit suit : Suit.values())
        {
            for (Rank rank : Rank.values())
            {
                deck.add(new Card(rank, suit));
            }
        }
    }
    // returns a copy of the deck    
    public static ArrayList<Card> newDeck()
    {
        return new ArrayList<Card>(deck);
    }

    public Rank getRank()
    {
        return rank;
    }

    public Suit getSuit()
    {
        return suit;
    }

    public String toString()
    {
        return rank +" of "+ suit;
    }

    public static void main(String[] args)
    {
        System.out.println(deck.toString());
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

不知所踪 2024-11-01 05:55:23

很简单,在您的 main 方法中,您只是一个 toString() 一个空的 ArrayList。您根本没有在 main 方法中调用 initDeck() 方法。

当您运行程序时 private static Final List; Deck = new ArrayList();
,牌组被分配了一个空的ArrayList

It's simple, in your main method, you're just toString() an empty ArrayList. You haven't called initDeck() method in your main method at all.

When you ran your program private static final List<Card> deck = new ArrayList<Card>();
, deck was assigned an empty ArrayList.

嗼ふ静 2024-11-01 05:55:23

非常可疑的是,您的甲板是私有静态最终的,而您的 init 方法是公共的并且是非静态的。它可以初始化多次并包含比您预期更多的卡片。您可能想在静态块中进行初始化。

我将替换代码如下:

  1. 使牌组 public static final 且不可修改(通过集合)
  2. 在静态初始值设定项中复制 init 方法的主体,如下所示:

<前><代码>静态{
for (西装套装: Suit.values()) {
for (Rank 等级 : Rank.values()) {
Deck.add(新卡(等级, 花色));
}
}
}

  1. 完全删除 initDeck

it is very suspicious that your deck is private static final and your init method public and non-static. It can be initialized several times and contain more cards then you would expect. You may want to do the initialization in static block instead.

I would replace the code as follows:

  1. make the deck public static final and unmodifiable (via Collections)
  2. copy the body of your init method in static initializer like this:
static {
  for (Suit suit : Suit.values()) {
    for (Rank rank : Rank.values()) {
      deck.add(new Card(rank, suit));
    }
  }
}
  1. delete initDeck completely
回忆那么伤 2024-11-01 05:55:23

这是因为 initDeck() 没有在任何地方被调用。尝试将 public void initDeck() 替换为 static

It's because initDeck() isn't called anywhere. Try to replace public void initDeck() with static.

你的背包 2024-11-01 05:55:23

List 定义中的关键字 final 用于具有无法更改的起始值的对象。

请在此处查看

The keyword final in you List<card> definition is for object that will have an starting value that cannot be changed.

Check it here

棒棒糖 2024-11-01 05:55:22

您的问题是您从未真正调用 initDeck,因此牌组保持为空,就像静态初始化程序运行时一样:

private static final List<Card> deck = new ArrayList<Card>();

其他问题:

  1. initDeck() 是一个实例方法,但 deck 是静态引用。
  2. initDeck() 是 Card 上的一个方法。
  3. deckCard 的静态成员,但卡不拥有或定义牌组。
  4. 面对静态的最终牌组,newDeck 是毫无意义的。
  5. 无论如何,卡牌不应该提供套牌。

简而言之,您的设计一团糟 - 您需要更多、更努力地思考实体及其相互关系。

Your problem is that you never actually invoke initDeck, so the deck remains empty, as it was when the static initializer ran:

private static final List<Card> deck = new ArrayList<Card>();

Other problems:

  1. initDeck() is an instance method, but deck is a static reference.
  2. initDeck() is a method on Card.
  3. deck is a static member of Card, but a card does not own or define a deck.
  4. newDeck is nonsensical in the face of a static final deck.
  5. Cards should not be delivering decks anyway.

In short, your design is messed up - you need to think more and harder about the entities and their inter-relationships.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文