我有java字符串数组,如下所示:
String [] cards = {"c1","c2","c3", , , , , , ,, , "c45"};
所以我有45个元素,现在我想每次重新输入它们 所以它将是:
int[] cards2 = Arrays.copyOf(cards , cards .length);
random(cards2);
随机函数应该怎么样?
答案 0 :(得分:3)
您可以使用
Collections.shuffle(Arrays.asList(cards));
答案 1 :(得分:1)
如果要复制到新阵列,可以使用此选项。
public String[] randome(String[] arr) {
Random rgen = new Random();
String[] randArray = new String[arr.length];
System.arraycopy(arr, 0, randArray, 0, arr.length);
for (int i = 0; i < randArray.length; i++) {
int randIn = rgen.nextInt(randArray.length);
String temp = randArray[i];
randArray[i] = randArray[randIn];
randArray[randIn] = temp;
}
return randArray;
}