在1000内取出900不同的随机数

1.首先是这样分析的:利用hashset的特性这个容器中保存的内容是按着集合的规则的排序的。
2.获取一个不可重复的容器
3.为容器添加900个不同的元素
4.再遍历容器
public static void main(String[] args) {
    Set<Integer> set = new HashSet<Integer>();
    while(set.size()<900){
        int num = (int) (Math.random()*1000);
        set.add(num);
    }
    for (Integer it: set) {
        System.out.print(it+" ");
    }
    System.out.println("***************"+set.size()+"**********");
}

猜你喜欢

转载自blog.csdn.net/heiiochange/article/details/81877261