Q:

Elements of ArrayList of Objects getting overwritten - Android

I am trying to initialize a deck of cards, shuffle them, then put them into a stack.
Somewhere between when I initialize them and when I shuffle them my data gets overwritten though.
Here's the code for my function:
public void shuffleDeck()
{

    ArrayList<Card> temp = new ArrayList<>();
    Card init = new Card();
    int counter = 0;
    // Initialize the deck
    Log.d("Shuffling Deck", "Starting now");
    for(int i=1;i<14;i++)
    {
        for(int j=1;j<5;j++)
        {
            init.setValue(i);
            init.setSuit(j);
            //Log.d("Value", Integer.toString(i));
            //Log.d("Suit", Integer.toString(j));
            temp.add(init);
            Log.d("Deck Build", "Suit: " + Integer.toString(temp.get(counter).getSuit()) + " ,Value: " + Integer.toString((temp.get(counter).getValue())));
            counter++;
        }
    }

    Card a = new Card();
    int index;
    Random rnd = ThreadLocalRandom.current();
    for(int l=temp.size()-1; l>0; l--)
    {

        index = rnd.nextInt(l+1);
        //Log.d("Deck Pre-Shuffle", "Suit: " + Integer.toString(temp.get(l).getSuit()) + " ,Value: " + Integer.toString((temp.get(l).getValue())));
        a = temp.get(index);
        temp.set(index,temp.get(l));
        temp.set(l,a);
        Log.d("Deck Shuffle", "Suit: " + Integer.toString(temp.get(l).getSuit()) + " ,Value: " + Integer.toString((temp.get(l).getValue())));
    }

    // Move deck to a stack
    for(int k=0; k<52; k++)
    {
        Log.d("Deck Push", "Suit: " + Integer.toString(temp.get(k).getSuit()) +" ,Value: " + Integer.toString((temp.get(k).getValue())));
        deck.push(temp.get(k));
    }
}

And the Card class has the two properties, value and suit, both ints.
When I'm logging for the "Deck Build" it looks like this, as expected (I cut it short for you):
Deck Build﹕ Suit: 1 ,Value: 1
Deck Build﹕ Suit: 2 ,Value: 1
Deck Build﹕ Suit: 3 ,Value: 1
Deck Build﹕ Suit: 4 ,Value: 1
Deck Build﹕ Suit: 1 ,Value: 2
Deck Build﹕ Suit: 2 ,Value: 2
Deck Build﹕ Suit: 3 ,Value: 2
Deck Build﹕ Suit: 4 ,Value: 2

But then the logging during the shuffling loop looks like this:
Deck Shuffle﹕ Suit: 4 ,Value: 13
Deck Shuffle﹕ Suit: 4 ,Value: 13
Deck Shuffle﹕ Suit: 4 ,Value: 13
Deck Shuffle﹕ Suit: 4 ,Value: 13

Any Ideas on what's happening?

A:

In your code 
ArrayList<Card> temp = new ArrayList<>();
Card init = new Card();
int counter = 0;
// Initialize the deck
Log.d("Shuffling Deck", "Starting now");
for(int i=1;i<14;i++)
{
    for(int j=1;j<5;j++)
    {
        init.setValue(i);
        init.setSuit(j);
        //Log.d("Value", Integer.toString(i));
        //Log.d("Suit", Integer.toString(j));
        temp.add(init);
        Log.d("Deck Build", "Suit: " + Integer.toString(temp.get(counter).getSuit()) + " ,Value: " + Integer.toString((temp.get(counter).getValue())));
        counter++;
    }
}

Only one Card object is created, so in the for loop you are just changing its value/suite and adding the same object to the ArrayList. The solution is simply creating a new Card object in 
for(int j=1;j<5;j++) {
    Card init = new Card();
    ...
}

