JAVA随机生成数组(不重复)
将一组数组顺序打乱,基于java.Vector容器。
public static Vector<Integer> getRandomRes(Vector<Integer> source,int size){
Vector<Integer> result = new Vector<Integer>();
Random random = new Random();
for (int i = 0; i < size - 1; i++) {
int randomIndex = random.nextInt(source.size() - i);
int randomRes = source.elementAt(randomIndex);
result.add(randomRes);
int temp = source.elementAt(randomIndex);
source.set(randomIndex, source.elementAt(source.size() - 1 - i));
source.set(source.size() - 1 - i, temp);
}
result.add(source.elementAt(0));
return result;
}
|