Search code examples
javaarraysrandomshufflepoker

Shuffling array of objects in Java: Why does this method work for int but not for my objects?


I know, I know - there are hundreds of people on here asking how to shuffle and array 52 card objects in Java and I thought it would be simple enough. I've not used collections (based on advice on stack). I've tried the Fisher-Yates shuffle but I'm seeing what looks like really odd behaviour... Can anyone help?

I based my solution on this answer: Random shuffling of an array

And it works great... for numbers. This Class has two arrays, one containing the numbers from 0 to 51 and the other containing 52 Card objects with various instance variables like suit and number.

This code works flawlessly:

public void shuffleNums()
   {
       Random rnd = new Random();
       for (int i = this.numbers.length -1; i > 0; i--)
       {
          int index = rnd.nextInt(i + 1);
          // Simple swap
          int temp = this.numbers[index];
          this.numbers[index] = this.numbers[i];
          this.numbers[i] = temp;

       }
   }

But when I try to use the same approach on a deck of 52 Card objects, like this:

public void shuffleCards()
   {
       Random rnd = new Random();
       for (int i = this.cards.length - 1; i > 0; i--)
       {
          int index = rnd.nextInt(i + 1);
          // Simple swap
          Card temp = this.cards[index];
          this.cards[index] = this.cards[i];
          this.cards[i] = temp;

       }
   }

It doesn't work so nice. Most of the cards are shuffled successfully but apparently I've dropped half the deck because now the array has about 16 NULL values and only about 36 random cards. The NULLs seem to be randomly distributed throughout the array.

I could easily work around this but I want to understand why this is happening. Can anyone help me work out what's going on?

EDIT: Regarding how I initialised the array - I had checked that the array did in fact have 52 Card objects listed from 2 of clubs up to Ace of spades. Before executing shuffle() the array correctly contained 52 card objects. Afterwards it contained roughly 36 - give or take.


Solution

  • It might be a problem with you initialized cards.

    If you do

    int[] cards = new int[52];
    

    All inside the int array will be set 0 which means you can perform operations on it

    However if you do

    Card[] cards = new Card[52];
    

    These are all set to null so if you to perform an operation on one of these cards, you get a null pointer exception. This may explain why you're getting all those null values.