We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,415 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Java array into ArrayLIst

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?

2
Contributors
3
Replies
11 Hours
Discussion Span
7 Months Ago
Last Updated
4
Views
Question
Answered
Yoink
Light Poster
26 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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.

JamesCherrill
... trying to help
Moderator
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

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])) );

            }
        }

    }
Yoink
Light Poster
26 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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));
     }
  }
JamesCherrill
... trying to help
Moderator
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30
Question Answered as of 7 Months Ago by JamesCherrill

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0687 seconds using 2.74MB