【发布时间】:2018-07-05 07:51:39
【问题描述】:
我需要使用 ArrayList 和嵌套的 for 循环打印出一副包含 52 张独特卡片的卡片。我只使用数组就完成了这项工作,但我无法使用 ArrayList 代替。任何帮助表示赞赏,谢谢。
实例变量:
public final String[] SUITS = {"Hearts", "Diamonds", "Spades", "Clubs"};
public final String[] DESCRIPTIONS = {"Ace", "Two", "Three", "Four",
"Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Jack", "Queen",
"King"};
private ArrayList<Card> deck;
将我的牌组添加到 ArrayList 的方法:
public void loadDeck1()
{
deck = new ArrayList<Card>();
for(int i = 0; i < DESCRIPTIONS.length; i++)
{
for(int j =0; j < SUITS.length; j++)
{
**deck.add(new Card(DESCRIPTIONS[i], SUITS[j]));
// BlueJ Error: actual and formal argument lists differ in length**
}
}
}
打印甲板的方法:
public void printDeck()
{
for(Card c : deck)
{
System.out.println(c.getSuit() + " of " + c.getDescription());
}
}
编辑:对不起,这是我的卡片类!
public class Card
{
private String suit;
private String description;
/**
* Constructor for objects of class Card
*/
public Card()
{
suit = null;
description = null;
}
/**
* Accessors
*/
/**
* @return the suit of the card
*/
public String getSuit()
{
return this.suit;
}
/**
* @return the description of the card
*/
public String getDescription()
{
return this.description;
}
/**
* Mutators
*/
/**
* @param the suit of the card
*/
public void setSuit(String suit)
{
this.suit = suit;
}
/**
* @param the description of the card
*/
public void setDescription(String description)
{
this.description = description;
}
}
【问题讨论】:
-
你需要一个
Card中的构造函数,它需要两个Strings。 -
@Stymieceptive 你没有列出你的 Card 类实现,你有吗?
-
你的构造函数是什么样子的?似乎参数的数量不匹配,您需要@notyou 提到的 2 个参数。
-
对不起,是的,我有一个!刚刚编辑了它。
标签: java arraylist printing nested-loops playing-cards