Hi, I'm haveing trouble turing an array of Card objects into an ArrayList. The main reason I would like to use an array list is because I can then use the get/remove/add methods associated with an array list which is much harder to do with a normal java array.

Heres my code

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;

public class Deck {
    int index;
    public Card[] deck;
    static final int NUM_OF_CARDS = 52;
    private static Rank[] rankValues = Rank.values();
    private static Suit[] suitValues = Suit.values();

    public Deck() {
        deck = new Card[52];
        int x = NUM_OF_CARDS - 1;
        for (int i = 0; i < suitValues.length; i++) {
            for (int j = 0; j < rankValues.length; j++) {
                deck[x] = new Card(rankValues[j], suitValues[i]);
                x--;
            }
        }

    }

    public void shuffle() {
        // Shuffles the deck.
        Card temp;
        int a;

        for (int x = 0; x < NUM_OF_CARDS; x++) {
            Random rnd = new Random();
            rnd.setSeed(123456);

            a = (int) (Math.random() * NUM_OF_CARDS);
            // a = b * NUM_OF_CARDS;
            temp = deck[x];
            deck[x] = deck[a];
            deck[a] = temp;
        }
    }


    public String toString() {
        return Arrays.toString(deck);
    }
    public static void main(String[] args) {
        Deck deck1 = new Deck();

        ArrayList<Deck> deckArray = new ArrayList<Deck>(Arrays.asList(deck1));

        System.out.println(deck1.toString());
        System.out.println(deckArray.toString());
        deckArray.remove(0);
        System.out.println(deckArray.toString());
        // System.out.println("The card on the top of the deck is: "+


        // deck1.shuffle();
        // System.out.println(deck1.toString());

    }

}

This is my output when I run the class testing main
[ks, qs, js, ts, 9s, 8s, 7s, 6s, 5s, 4s, 3s, 2s, as, kh, qh, jh, th, 9h, 8h, 7h, 6h, 5h, 4h, 3h, 2h, ah, kd, qd, jd, td, 9d, 8d, 7d, 6d, 5d, 4d, 3d, 2d, ad, kc, qc, jc, tc, 9c, 8c, 7c, 6c, 5c, 4c, 3c, 2c, ac]

[[ks, qs, js, ts, 9s, 8s, 7s, 6s, 5s, 4s, 3s, 2s, as, kh, qh, jh, th, 9h, 8h, 7h, 6h, 5h, 4h, 3h, 2h, ah, kd, qd, jd, td, 9d, 8d, 7d, 6d, 5d, 4d, 3d, 2d, ad, kc, qc, jc, tc, 9c, 8c, 7c, 6c, 5c, 4c, 3c, 2c, ac]]

[]

Which makes me believe its makeing a list array that only contains one object which ends up being all my objects in my previous array combined. How would I make the ArrayList separate each element in my array into its own object?

Recommended Answers

All 3 Replies

deck1 is a single object, it is NOT an array of cards. It happens to contain an array of cards, but that's not relevent except to how it's printed out. So deckArray is an array list of 1 element, an instance of Deck. When you remove that 1 element the arraylist is empty.
If you want an arraylist of cards you will need an accessor method in Deck that returns the array of Cards. Maybe it would be better to have Deck contain an ArrayList<Card> rather than a Card[], especially if you want to remove Cards from the Deck etc.

This is what I tried next in my deck constructor to just make my card objects straight into the arrayList, but when I test to see what it prints out all I'm getting back is a null.

    public Deck() {

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

        for (int i = 0; i < suitValues.length; i++) {
            for (int j = 0; j < rankValues.length; j++) {
                deckArray.add((new Card(rankValues[j], suitValues[i])) );

            }
        }

    }

That code looks ok - presumably th eproblem is in the print?

ps you don't need all those arrays and indexing to loop thru an enum. Just an enhanced for loop will do, and is much cleaner simpler code, eg

  for (Suit s : Suit.values()) {
     for (Rank r : Rank.values()) {
        deck.add(new Card(s, r));
     }
  }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.