A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 齐银春 中级黑马   /  2012-12-1 21:43  /  1942 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

用hashSet求10 0到30之间的无重复随机数
public static void main (String args[]) {
  
   Random rand=new Random();
  Set<Integer> intset=new HashSet<Integer>();
  for (int i = 0; i < 10; i++) {
   intset.add(rand.nextInt(30));
   System.out.println(intset);
  }

结果却是下面的,不知为何,求解

未命名.jpg (11.42 KB, 下载次数: 54)

未命名.jpg

6 个回复

倒序浏览
你操作的都是一个hashset集合。也就是说你每次放入的值都在这个集合里。set集合的特点是元素唯一无序。所以你获得的随机数如果是集合里已经有的话。那么这个数就不会放进去,所以实际上确实是得到了十个随即数。但是因为set集合唯一性的特点。有重复的值并没有放到hashset集合里。所以,你最后只获得了七个随机数。还有就是你把输出语句放到for循环里写的话,每次循环执行的 时候都会打印这个集合,打印出集合里的元素。所以你打印了十个集合。建议重写hashcode方法和equals方法。或者不用hashset集合,使用其他的集合。

评分

参与人数 1技术分 +1 收起 理由
冯海霞 + 1

查看全部评分

回复 使用道具 举报
  1. import java.util.*;
  2. class HashSetRandom//用hashSet存储十个0到30的随机数
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 HashSet<Integer> hs=new HashSet<Integer>();
  7.                 Random r=new Random();
  8.                 while(hs.size()<10)
  9.                 {
  10.                         hs.add(r.nextInt(30));
  11.                 }
  12.                 System.out.println(hs);
  13.         }
  14. }
复制代码

用hashset存储10个随机数.jpg (156.62 KB, 下载次数: 58)

用hashset存储10个随机数.jpg

评分

参与人数 1技术分 +1 收起 理由
冯海霞 + 1

查看全部评分

回复 使用道具 举报
王震阳 发表于 2012-12-2 10:51

领教了 谢谢
回复 使用道具 举报
  1. public class RandomDemo {
  2.         public static void main(String[] args) {
  3.                 // 定义一个长度为10的数组
  4.                 int[] arr = new int[10];

  5.                 // 往数组里添加随机数
  6.                 for (int i = 0; i < 10; i++) {
  7.                         int num = (int) (Math.random() * 30);
  8.                         arr[i] = num;
  9.                         // 判断是否有相同值
  10.                         for (int j = 0; j < i; j++) {
  11.                                 // 有则在此索引处重新添加一个随机数
  12.                                 if (arr[i] == arr[j]) {
  13.                                         i--;
  14.                                 }
  15.                         }
  16.                 }
  17.                 // 遍历
  18.                 for (int i = 0; i < arr.length; i++) {
  19.                         if (i == arr.length - 1) {
  20.                                 System.out.print(arr[i]);
  21.                         } else {
  22.                                 System.out.print(arr[i] + " ");
  23.                         }
  24.                 }
  25.         }
  26. }
复制代码
回复 使用道具 举报
添加1-30随机数
1.用hashSet 底层是链表根据hashcode来排序 所以添加的元素还是有一定的规律的  如果添加1-30的数  可以说是随即 也可以说不是   因为它们的排列顺序是固定的 可以通过下面方式实现添加随即数
main(){
    HashSet set=new HashSet(30);
  for(int i=0;i<30;i++){
    Num n=new Num(i);
   set.add(n);  
}
}
class Num{
   public Num(int num){
   this.num=num;
}
    public int num;
    @Override
  public int hashCode() {
  return new Random().nextInt(Integer.maxValue);
}
}

2.  用treeset 或list集合  自己写个比较器 list的话 collections.sort排序下  如果是数组  Arrays.sort 排序下
main(){
   Comparator<Integer> c=new Comparator<Integer>() {
   @Override
   public int compare(Integer o1, Integer o2) {
    return new Random().nextBoolean()?1:-1;
   }
  };
  TreeSet set=new TreeSet(c);
  for(int i=0;i<30;i++){
   set.add(i+1);
  }
  System.out.println(Arrays.toString(set.toArray()));
}

3.  

  int[] arrs=new int[30];
  for(int i=0;i<30;i++){
   arrs[i]=i+1;
  }
  
  for(int i=0;i<30;i++){
   int position=arrs.length-i-1;
   int j=new Random().nextInt(position+1);
   if(j!=position){
    int tmp=arrs[j];
    arrs[j]=arrs[position];
    arrs[position]=tmp;
   }
   
  }
  System.out.println(Arrays.toString(arrs));




回复 使用道具 举报
因为HashSet集合的数据结构是哈希表。保证元素的唯一性这是它的一大特性。所以你运行for循环的时候不一定能存进10个不同元素的对象。碰到相同元素的对象那么它就不会存储了。
这道题你最好用while来做。用标记的形式。当hashSet的size方法大于等于10的时候改变标记跳出while循环即可。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马