Java распечатывает колоду карт с ArrayList и вложенными для циклов - PullRequest
0 голосов
/ 05 июля 2018

Мне нужно распечатать колоду из 52 уникальных карт, используя ArrayList и вложенный цикл for. Я сделал это только с помощью массивов, но я не могу вместо этого обернуть голову, используя 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());
    }
}

edit: Извините, вот мой класс карт!

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;
}    

}

1 Ответ

0 голосов
/ 06 июля 2018

Вам нужно добавить конструктор, который принимает две строки (масть, описание) и создать объект Card для класса Card, потому что вы будете использовать конструктор в выражении deck.add(new Card(DESCRIPTIONS[i], SUITS[j]));. Если вы не добавите этот конструктор, вы можете изменить описание или Переменная масти объекта карты с помощью метода setMethods.

public Card(String s , String d)
{
    suit = s;
    description = d;
}

Если вы хотите использовать ArrayList вместо массивов для переменных экземпляра.

public ArrayList<String> SUITS = add("Hearts");
ArrayList<String> SUITS = add("Diamonds");
ArrayList<String> SUITS = add("Spades");
ArrayList<String> SUITS = add("Clubs");

public ArrayList<String> DESCRIPTIONS = add("Ace");/...
//and add elements of descriptions arrayList

Вы также можете передать ArrayList обычный цикл for вместо каждого цикла.

Требуется изменить некоторую часть кодов:

for(int i = 0; i < DESCRIPTIONS.size(); i++)
{
    for(int j =0; j < SUITS.size(); j++)
    {
        **deck.add(new Card(DESCRIPTIONS.get(i), SUITS.get(j))); 
    }
} 
...