[英]ArrayList is empty but why?
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 ). 我正在尝试将卡添加到ArrayList卡,但它似乎不起作用(大多数代码是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());
}
}
Your problem is that you never actually invoke initDeck, so the deck remains empty, as it was when the static initializer ran: 你的问题是你从未真正调用initDeck,因此当静态初始化程序运行时,套牌仍为空:
private static final List<Card> deck = new ArrayList<Card>();
Other problems: 其他问题:
initDeck()
is an instance method, but deck
is a static reference. initDeck()
是一个实例方法,但是deck
是一个静态引用。 initDeck()
is a method on Card. initDeck()
是Card上的一种方法。 deck
is a static member of Card
, but a card does not own or define a deck. deck
是Card
的静态成员,但是卡不拥有或定义甲板。 newDeck
is nonsensical in the face of a static final deck. 面对静态的最终套牌, newDeck
是荒谬的。 In short, your design is messed up - you need to think more and harder about the entities and their inter-relationships. 简而言之,你的设计搞砸了 - 你需要更多地思考实体及其相互关系。
it is very suspicious that your deck
is private static final
and your init method public
and non-static
. 你的deck
是private static final
,你的init方法是public
和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: 我会替换代码如下:
public static final
and unmodifiable (via Collections) 使甲板public static final
和不可修改(通过集合) static { for (Suit suit : Suit.values()) { for (Rank rank : Rank.values()) { deck.add(new Card(rank, suit)); } } }
initDeck
completely 完全删除initDeck
It's simple, in your main
method, you're just toString()
an empty ArrayList
. 这很简单,在你的main
方法中,你只是toString()
一个空的ArrayList
。 You haven't called initDeck()
method in your main
method at all. 您根本没有在main
方法中调用initDeck()
方法。
When you ran your program private static final List<Card> deck = new ArrayList<Card>();
当你运行你的程序private static final List<Card> deck = new ArrayList<Card>();
, deck was assigned an empty ArrayList
. ,deck被分配了一个空的ArrayList
。
It's because initDeck()
isn't called anywhere. 这是因为initDeck()
不会在任何地方被调用。 Try to replace public void initDeck()
with static
. 尝试用static
替换public void initDeck()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.