在for循环中交换整数数组值 - 是否可能?

Dae*_*ric 2 java arrays integer

所以我决定创建一个简单的卡片组洗牌程序,没有什么可以只用一个单独的数组,52"卡"编号从0到52.我不一定确定它是否是最好的方法.

我创建了一个随机整数方法(randInt)来获取随机数和一个随机方法(shuffle),它获取两个随机卡并交换它们的值.

我的方法很简单但是当我运行代码(以及我添加的一些调试代码)时,我似乎得到每个值变成完全相同的值(d [2] = d [1] = randCard1),这导致相同的倍数卡和显然其他价值观完全消失.如果给予足够的洗牌,整个牌组可能会变成52张牌,所有牌都由相同的值组成.

我已经阅读了一些关于交换整数数组的问题,但我的问题是不同的,因为它是关于使用for循环的问题,(因为我的交换数组值的方法已经类似于那些问题,但是final/static关键字没有帮助当我修改几次时,我得出结论,循环是责备)这显然会阻止阵列正确更新.

如果是这样的话,我怎么过来这个,如果不是我错过了什么?我交换整数数组值的方式还有其他问题吗?

这是我的代码:

import java.util.Random;

public class Cards {

    public static void main(String[] args) {

        int[] deck = new int[] {
            0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
            10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
            20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
            30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
            40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
            50, 51
        };

        printArray(deck, "Original Deck:");
        deck = shuffle(deck);
        printArray(deck, "Shuffled Deck:");
        }

        public static int[] shuffle(int[] d) {
            //shuffle between 100 to 200 to ensure a thorough shuffle
            int randLoops = randInt(100, 200);
            int randCard1;
            int randCard2;

        for (int i = 0; i <= randLoops; i++) {
            int temp;

            randCard1 = randInt(0, 51);

            do {randCard2 = randInt(0, 51);}
            while (randCard2 == randCard1);

            System.out.print("*" + randCard1 + "|" + randCard2 + "/");
            temp = randCard1;
            d[randCard1] = d[randCard2];
            d[randCard2] = d[temp];
            System.out.println(d[randCard1] + "|" + d[randCard2] + "*");
        }
        return d;
    }

    public static int randInt(int min, int max) {
        Random rand = new Random();    
        int randomNum = rand.nextInt((max - min) + 1) + min;

        return randomNum;
    }

    public static void printArray(int[] d, String t) {
        System.out.println(t);
        for (int i = 0; i < d.length; i++) {
                System.out.print(d[i] + ", ");
        }

        System.out.println();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的输出调试代码示例(希望它有帮助,但不会阻碍):

Original Deck:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 
*40|41/41|41*
*29|40/41|41*
*19|26/26|26*
*2|27/27|27*
*4|30/30|30*
*24|25/25|25*
*7|1/1|1*
*17|47/47|47*
*14|5/5|5*
*42|11/11|11*
*23|42/11|11*
*33|30/30|30*
*19|42/9|9*
*11|26/9|9*
*40|21/9|9*
*36|11/9|9*     <--------- at the end I don't know why the values = 9?
Shuffled Deck:
9, 11, 30, 11, 30, 30, 31, 45, 11, 11, 51, 9, 11, 45, 45, 45, 51, 11, 9, 9, 44, 9, 30, 44, 27, 11, 9, 27, 31, 11, 11, 21, 11, 11, 45, 35, 9, 11, 11, 30, 9, 11, 9, 11, 21, 31, 9, 30, 45, 30, 11, 45, 
BUILD SUCCESSFUL (total time: 2 seconds)
Run Code Online (Sandbox Code Playgroud)

JB *_*zet 6

这就是问题所在:

temp = randCard1;
d[randCard1] = d[randCard2];
d[randCard2] = d[temp];
Run Code Online (Sandbox Code Playgroud)

您可以保存其索引,而不是将卡值保存在临时值中.你需要改为

temp = d[randCard1];
d[randCard1] = d[randCard2];
d[randCard2] = temp;
Run Code Online (Sandbox Code Playgroud)

请注意,JDK有一个shuffle方法:

// create the deck
Integer[] deck = new Integer[52];
for (int i = 0; i < deck.length; i++) {
    deck[i] = i;
}

// shuffle it
Collections.shuffle(Arrays.asList(deck));
Run Code Online (Sandbox Code Playgroud)