ArrayList 是空的,但是为什么呢?
我正在尝试将卡片添加到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
很简单,在您的
main
方法中,您只是一个toString()
一个空的ArrayList
。您根本没有在main
方法中调用initDeck()
方法。当您运行程序时
private static Final List; Deck = new ArrayList();
,牌组被分配了一个空的ArrayList
。It's simple, in your
main
method, you're justtoString()
an emptyArrayList
. You haven't calledinitDeck()
method in yourmain
method at all.When you ran your program
private static final List<Card> deck = new ArrayList<Card>();
, deck was assigned an emptyArrayList
.非常可疑的是,您的甲板是私有静态最终的,而您的 init 方法是公共的并且是非静态的。它可以初始化多次并包含比您预期更多的卡片。您可能想在静态块中进行初始化。
我将替换代码如下:
public static final
且不可修改(通过集合)initDeck
it is very suspicious that your
deck
isprivate static final
and your init methodpublic
andnon-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)initDeck
completely这是因为
initDeck()
没有在任何地方被调用。尝试将public void initDeck()
替换为static
。It's because
initDeck()
isn't called anywhere. Try to replacepublic void initDeck()
withstatic
.List
定义中的关键字final
用于具有无法更改的起始值的对象。请在此处查看
The keyword
final
in youList<card>
definition is for object that will have an starting value that cannot be changed.Check it here
您的问题是您从未真正调用 initDeck,因此牌组保持为空,就像静态初始化程序运行时一样:
其他问题:
initDeck()
是一个实例方法,但deck 是静态引用。
initDeck()
是 Card 上的一个方法。deck
是Card
的静态成员,但卡不拥有或定义牌组。newDeck
是毫无意义的。简而言之,您的设计一团糟 - 您需要更多、更努力地思考实体及其相互关系。
Your problem is that you never actually invoke initDeck, so the deck remains empty, as it was when the static initializer ran:
Other problems:
initDeck()
is an instance method, butdeck
is a static reference.initDeck()
is a method on Card.deck
is a static member ofCard
, but a card does not own or define a deck.newDeck
is nonsensical in the face of a static final deck.In short, your design is messed up - you need to think more and harder about the entities and their inter-relationships.