JAVA随机生成数组(不重复)
将一组数组顺序打乱,基于java.Vector容器。
public static VectorgetRandomRes(Vector source,int size){ Vector result = new Vector (); 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; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)