了解如何将值传递给方法并从方法返回

时间:2015-03-29 02:44:55

标签: java

我有点困惑。我似乎得到了不同的结果。我已经阅读了其他问题,但为什么我能够在第一种方法中看到数组但在第二种方法中却看不到?

我将一个数组从main方法传递给另一个没有返回值的方法,但似乎传递的数组能够被main方法访问,因为数组是从main方法打印的。这就像指针吗?

我再次尝试使用main的另一个方法调用,结果不一样。

对此的任何见解都会很棒。提前致谢!

/ **  *此类提供了一种测试混洗方法的便捷方法。  * / 公共类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();
}

}

0 个答案:

没有答案