Chinaunix首页 | 论坛 | 博客
  • 博客访问: 202522
  • 博文数量: 73
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 750
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-13 18:32
文章分类

全部博文(73)

文章存档

2009年(1)

2008年(72)

我的朋友

分类: Java

2008-03-17 09:18:54

  使用一个临时数组,通过不断减小生成的数组索引实现:

public static int[] noRepRandoms(){
    int[] tmpArray=new int[10];
    for(int i=0;i         tmpArray[i]=i;
    int[] results=new int[10];
    Random rd=new Random();
    int tmpIndex=0,len=tmpArray.length;
    for(int i=results.length-1;i>=0;i--){
        tmpIndex=Math.abs(rd.nextInt()%len);
        len--;
        results[i]=tmpArray[tmpIndex];
        tmpArray[tmpIndex]=tmpArray[len];
    }

使该函数可执行,运行结果:

C:\javawork\j2se\test>java Test2
6 1 9 5 4 7 8 2 3 0



阅读(666) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~