1

I want to create a program that creates a deck of cards and to have an array of cards. Each card is also an array that holds two values, suit and value.

So you would be able to do something like card[1] = [1,5] where 1 is the rank of the suite and 5 is the value of the card.

It's been a long time since I have used Java so this is my project to reteach me how to program.

Thanks in advance for the help!

jmj
  • 237,923
  • 42
  • 401
  • 438
louvillian
  • 11
  • 1

3 Answers3

8
int[][] cards = new int[52][2];

So then cards[0] would be the value of the first card, an array with two elements. cards[0][0] would be the suit of the first card, for example (if suit comes before value).

But consider making an array of Card objects, and have the Card object have two fields. It has the advantage of being less confusing (will you always remember which comes first), is easier to read (cards[0].getSuit() is nicer than cards[0][0]), and if you want to add any more accessory data, it'll be easier than having to tack on a third element to the array.

Sherz
  • 568
  • 2
  • 14
0

Creating an array of card objects would be better as it is more human readable. You could create an ArrayList of Card objects as such:

ArrayList<Card> deck = new ArrayList<Card>();

Your card class could appear as below, I chose Strings for value and suit, but you could use ints.

public class Card
{

    private String suit;
    private String value;

    public Card(String suit, String value)
    {
        this.suit = suit;
        this.value = value;
    }

    public String getSuit()
    {
        return suit;
    }

    public void setSuit(String suit)
    {
        this.suit = suit;
    }

    public String getValue()
    {
        return value;
    }

    public void setValue(String value)
    {
        this.value = value;
    }

}
Duane
  • 1,980
  • 4
  • 17
  • 27
0

Here is one try:

//declare a class
public class Card
{
   public String[] myStringArray1 = new String[10];
}

//declare an arrayof type Card
public Card[] myStringArray2 = new Card[10];

The myStringArray2 is an array of array.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
Ajee
  • 1
  • 2