public class Try {
public static void main(String[] args) {
final int Num = 52;
int[] cards = new int[Num];
for (int i = 0; i < cards.length; i++) {
cards[i] = i;
}
System.out.println("洗牌前:");
for (int i = 0; i < cards.length; i++) {
System.out.print(cards[i]);
if ((i + 1) % 13 == 0) {
System.out.println();
} else {
System.out.print(", ");
}
}
// 洗牌
for (int i = 0; i < cards.length; i++) {
int newIndex = (int) (Math.random() * Num);// 0-51之间 有重复且无序的数字
int temp = cards[i];// 0-51有序、无重复数字
cards[i] = cards[newIndex];
cards[newIndex] = temp;
}
System.out.println("洗牌后:");
for (int i = 0; i < cards.length; i++) {
System.out.print(cards[i]);
if ((i + 1) % 13 == 0) {
System.out.println();
} else {
System.out.print(", ");
}
}
}
}
在洗牌的代码段中,为什么去掉int temp = cards[i]和cards[newIndex] = temp;这段代码,会导致洗牌后的数组中出现重复的数字?