题目
随机打乱Vector的顺序输出
代码
package com.graph;
import java.util.*;
public class Solution{
Random rand = new Random();
int[] pos;
Vector<Integer> nums;
int n;
public Solution(Vector<Integer> nums){
this.nums = nums;
n = nums.size();
pos = new int[n];
for(int i=0; i<n; i++){
pos[i] = i;
}
}
public void myShuffle(){
for(int i=n-1; i>=0; i--){
int changeIndex = rand.nextInt(i+1);//生成[0,i]间的随机数
int tmp = pos[i];
pos[i] = pos[changeIndex];
pos[changeIndex] = tmp;
}
for(int i=0; i<n; i++){
System.out.print(nums.get(pos[i]));
}
}
public void javaShuffle() {
List<Integer> tmp = new ArrayList<Integer>(nums);
Collections.shuffle(tmp);
for(int i=0; i<n; i++){
System.out.print(tmp.get(i));
}
}
}