1

This code is supposed to perform a "perfect shuffle" on a deck of cards. It represents the division of a deck into two decks and then the subsequent interleaving of them.
When I pass in an array and then print out the elements in the returned array, the elements are evidently shuffled. However, as the array in the perfectShuffle method is an alias of whatever array I pass into it, the original array back in the main method should also be "shuffled".
However, when I print out the array after performing the perfect shuffle method on it, the elements are not shuffled. When I check the same returned array, it is shuffled... I'm not sure why it isn't working. Could someone explain this to me?

public class Shuffler {

private static final int SHUFFLE_COUNT = 1;

private static final int VALUE_COUNT = 9;

public static void main(String[] args) {
System.out.println("Results of " + SHUFFLE_COUNT +
                         " consecutive perfect shuffles:");
  int[] values1 = new int[VALUE_COUNT];

  for (int i = 0; i < values1.length; i++) {
     values1[i] = i;
  }
  perfectShuffle(values1);

  for (int j = 1; j <= SHUFFLE_COUNT; j++) {
     System.out.print("  " + j + ":");
     for (int k = 0; k < values1.length; k++) {
        System.out.print(" " + values1[k]);
     }
     System.out.println();
  }
  System.out.println();
  }


  public static int[] perfectShuffle(int[] values) {
  int[] shuffle = new int[values.length];

  int k=0;

  for(int j=0; j<(values.length+1)/2; j++)
  {
     shuffle[k]=values[j]; 
     k+=2;  
  }

  k=1;
  for(int j=(values.length+1)/2; j<values.length; j++)
  {
     shuffle[k]=values[j];
     k+=2; 
  }
  values=shuffle;
  return values;

}
}
Devavrata
  • 1,785
  • 17
  • 30

1 Answers1

1

This is to do with arguments passed by reference and by value. Your code is a bit chaotic and is not clear to see the trouble at once. You pass array by value. Thus the pointer go the object is copied. This could work if you processed the items of this exact object. But you create a copy inside perfectShuffle() and the resulf is a reference to the new object. However you never use the return value in code of main()

EDIT: answer to comment doesn't fit comment size. Some explanations: Read some info about references to objects. In a nutshell, an object - array - occupies continuous block of memory while object reference is a variable of standart size that contains a pointer to the start of object's memory. There may be a lot of references pointing same object. When you call a function and pass an object reference, it is copied. Now there are 2 refs, one in main code, one in subfunction. If you modify the array inside function like arr[i] = value it would actually modify the object, and main reference 'sees' the modified object, because there's only one object. But when you overwrite the reference pointing array in a function, it has effect only inside the function, because it is local scope copy, and is not returned

Kirill Slatin
  • 6,085
  • 3
  • 18
  • 38
  • Sorry for the late response, but I know that I create a copy inside perfectShuffle(). The thing is that I set values to the copy again, but it doesn't show up back in the main. And I know that I don't use the returned value in the main. I used it in a separate program to test it, and didn't see the need to put it up here because I already said that the returned value is perfectly shuffled. – user3673010 Mar 06 '15 at 15:01