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?