随机选取多个元素,不重复(标识数组-外部)
package com.example.practicedemo;
import java.util.Arrays;
import java.util.Random;
public class IceCream {
private static Random random = new Random(47);
private static final String[] FLAVORS = {"Chocolate","StrawBerry","Vanilla Fudge Swirl",
"Mint Chip","Mocha Almond Fudge","Rum Raisin","Praline Cream","Mud Pie"};
public static String[] flavorSet(int n) {
if(n>FLAVORS.length) {
throw new IllegalArgumentException("Set too big");
}
String[] result = new String[n];
boolean[] picked = new boolean[FLAVORS.length];
for(int i=0;i<n;i++) {
int t;
do
t = random.nextInt(FLAVORS.length);
while(picked[t]);
result[i] = FLAVORS[t];
picked[t] = true;
}
return result;
}
public static void main(String[] args) {
for(int i=0;i<7;i++) {
System.out.println(Arrays.toString(flavorSet(3)));
}
}
}
运行结果:
[Rum Raisin, Mint Chip, Mocha Almond Fudge]
[Chocolate, StrawBerry, Mocha Almond Fudge]
[StrawBerry, Mint Chip, Mocha Almond Fudge]
[Rum Raisin, Vanilla Fudge Swirl, Mud Pie]
[Vanilla Fudge Swirl, Chocolate, Mocha Almond Fudge]
[Praline Cream, StrawBerry, Mocha Almond Fudge]
[Mocha Almond Fudge, StrawBerry, Mint Chip]