- I just want to preface this with that I have done my research & have found numerous articles pertaining to this issue both on here & on other sites
- examples include:
How to populate an array list in a while loop
Pass by value/reference, what?
Create several new objects within a for-loop in Java
And these have helped me identify my issue, but there isn't much on each threads on how to get around it, or resolve it. I assume it is possible. Anyway, in a nutshell I'm creating a Deck of cards program utilising two classes,
- 'Card' (Representing a single card within the deck, &
- 'Deck' (Representing the container for the 52 card objects, - will later contain methods to shuffle, deal, etc. )
Using the code below, I'm setting up a Deck object & instantiating an ArrayList, which I'm populating with the Card objects using a two-tier for loop. If I Sysout while iterating, it clarifies thjat the objects are getting "created" with the correct values, however at the end of the loops, calling the Card toString() method on the ArrayList reveals 52 Ace of Clubs objects - which is the last card to be "created". Obviously it isn't creating 52 separate Card objects, but rather referencing the same one 52 times, & because the last time the variables are altered to Ace of Clubs, it appears as if there's 52 of them.
I don't understand how they can all be the same object when I am explicitly creating a new Card object in my Deck constructor. Any help on what I need to refactor to achieve 52 distinct objects within my Arraylist each with their respective rank/suit populated correctly would be GREATLY appreciated.
for ease of clarity, I have stripped my program back to just the bare bones code causing this issue
cheers in advance.
import java.util.ArrayList;
public class Deck {
ArrayList<Card> mainDeck;
public Deck(){
mainDeck = new ArrayList<>();
for(int x = 1;x<=4;x++){
for(int y = 2;y<=14;y++){
mainDeck.add(new Card(y, x));
}
}
}
public void listCards(){
System.out.println(mainDeck.toString());
}
/** I have also tried the below method, which also fails!
*
* public Deck(){
mainDeck = new ArrayList<>();
for(int x = 1;x<=4;x++){
for(int y = 2;y<=14;y++){
Card card = new Card(y, x);
mainDeck.add(card);
}
}
}
*
*
*/
}
public class Card {
public static int rank;
public static int suit;
public static String rankText;
public static String suitText;
public Card(){
}
public Card(int newRank, int newSuit){
rank = newRank;
suit = newSuit;
switch(newSuit){
case 1:
suitText = "Spades";
break;
case 2:
suitText = "Hearts";
break;
case 3:
suitText = "Diamonds";
break;
case 4:
suitText = "Clubs";
break;
default:
break;
}
switch(newRank){
case 2:
rankText = "Two";
break;
case 3:
rankText = "Three";
break;
case 4:
rankText = "Four";
break;
case 5:
rankText = "Five";
break;
case 6:
rankText = "Six";
break;
case 7:
rankText = "Seven";
break;
case 8:
rankText = "Eight";
break;
case 9:
rankText = "Nine";
break;
case 10:
rankText = "Ten";
break;
case 11:
rankText = "Jack";
break;
case 12:
rankText = "Queen";
break;
case 13:
rankText = "King";
break;
case 14:
rankText = "Ace";
break;
}
}
public static int getRank() {
return rank;
}
public static int getSuit() {
return suit;
}
public static String getRankText() {
return rankText;
}
public static String getSuitText() {
return suitText;
}
@Override
public String toString() {
return "Card: = "+ getRankText() + " of "+ getSuitText() + "\n";
}
}
public class App
{
public static void main( String[] args )
{
Deck thisDeck = new Deck();
thisDeck.listCards();
}
}