1

I am a little baffled. I seem to be getting different results. I have read the other questions, but why am I able to see the array in the first method but not in the second?

I passed an array from the main method to another method that did NOT have a return value, yet it seems like the array passed was able to be accessed by the main method because the array was printed from the main method. Is this like a pointer??

I tried this again with another method call from main, and the result was not the same.

Any insight on this would be great. Thanks in advance!

/** * This class provides a convenient way to test shuffling methods. */ public class ShufflerCopy {

/**
 * The number of consecutive shuffle steps to be performed in each call
 * to each sorting procedure.
 */
private static final int SHUFFLE_COUNT = 2;


/**
 * Tests shuffling methods.
 * @param args is not used.
 */
public static void main(String[] args) {
    System.out.println("Results of " + SHUFFLE_COUNT +
                             " consecutive perfect shuffles:");
    int[] values1 = {0, 1, 2, 3};
    int[] values52 = new int[52];
    for (int i = 0; i<52; i++){
        values52[i]=i+1;
    }
    for (int j = 1; j <= SHUFFLE_COUNT; j++) {
        perfectShuffle(values52);
        System.out.print("  " + j + ":");
        for (int k = 0; k < values52.length; k++) {
            System.out.print(" " + values52[k]);
        }
        System.out.println();
    }
    System.out.println();    

    System.out.println("Results of " + SHUFFLE_COUNT +
                             " consecutive efficient selection shuffles:");


    int[] values2 = {0, 1, 2, 3};
    for (int j = 1; j <= SHUFFLE_COUNT; j++) {
        selectionShuffle(values52);
        System.out.print("  " + j + ":");
        for (int k = 0; k < values52.length; k++) {
            System.out.print(" " + values52[k]);
        }
        System.out.println();
    }
    System.out.println();        
}


/**
 * Apply a "perfect shuffle" to the argument.
 * The perfect shuffle algorithm splits the deck in half, then interleaves
 * the cards in one half with the cards in the other.
 * @param values is an array of integers simulating cards to be shuffled.
 */
//public static int[] perfectShuffle(int[] values) {
public static void perfectShuffle(int[] values) {
    int[] value = values; 
    int[] shuffled = new int[values.length]; //array of the shuffled integers
    for (int i=0; i< value.length/2;i++){
        shuffled[i*2]= value[i];//This will copy the values to the even numbered positions
    }
    int k =1;
    for (int i=value.length/2;i<value.length;i++){
        shuffled[k]=value[i];
        k = k+2;
    }
    for (int i=0; i<value.length;i++){
        value[i]=shuffled[i];
    }
    //return value;

}

/**
 * Apply an "efficient selection shuffle" to the argument.
 * The selection shuffle algorithm conceptually maintains two sequences
 * of cards: the selected cards (initially empty) and the not-yet-selected
 * cards (initially the entire deck). It repeatedly does the following until
 * all cards have been selected: randomly remove a card from those not yet
 * selected and add it to the selected cards.
 * An efficient version of this algorithm makes use of arrays to avoid
 * searching for an as-yet-unselected card.
 * @param values is an array of integers simulating cards to be shuffled.
 */
public static void selectionShuffle(int[] values) {
    int[] cards = new int[values.length];//creates an empty array of ints
    int[] shuffled = new int[values.length]; //array of the shuffled integers
    for (int k=0; k<values.length;k++){
        cards[k]=values[k]; //We will use 0 to indicate "empty"
        //System.out.print("cards["+k+"]:"+values[k]+" ");
    }
    System.out.println();
    for (int k=0; k<values.length;k++){  //pick a random card and put in the shuffled array

        int pickRandom = (int)(Math.random() * 52) ;//random generates a value from 0 to less than 1
        //System.out.println("k is: " +k+ " pickRandom: " +pickRandom);
        if (cards[pickRandom] != 0) {
            shuffled[k]=cards[pickRandom];
            //System.out.println("shuffled[k]: "+ shuffled[k]);
            cards[pickRandom] = 0;
            //System.out.println("For card "+k+ ": " +shuffled[k] +" was found on the first pick.");
        }
        else {
            int count = 1;
            do{
            //while (cards[pickRandom]==0){  //So if the card was a 0, try, try again 
                count ++;
                pickRandom = (int)(Math.random() * 52);  //pick a new random card 
                if (cards[pickRandom] != 0) {
                    shuffled[k]=cards[pickRandom];
                    cards[pickRandom]=0;
                    //System.out.println("For card "+k+ ": "+ shuffled[k] +" was found on pick number "+count);
                }//System.out.println("Outside if loop");
            }while (cards[pickRandom]!=0);
            //System.out.println("Outside while loop"); 
        }//System.out.println("Outside else loop");
    //System.out.println("incrementing k");
    }
    for (int i=0; i<cards.length;i++){
        cards[i]=shuffled[i];
        System.out.print(" "+cards[i]);
    }
    System.out.println();
}

}

Jeff Ciaccio
  • 83
  • 1
  • 7
  • Write a really simple program that illustrates the scenario you're asking about. If it's still not clear to you, post the simplified code. – Quicksilver002 Mar 29 '15 at 02:55

0 Answers0