利用随机函数生成10个随机数,并将它们存入到一个HashSet对象中,然后利用迭代器输出。
public static void main(String[] args) {
HashSet<Integer> list = new HashSet<>();
for (int i = 0; i < 10; i++) {//生成10个100内的随机数
Random r = new Random();
int a = r.nextInt(100); //转为int类型
list.add(a);
}
Iterator i = list.iterator();//获取迭代器
while (i.hasNext()) {//判断是否有下一个元素
System.out.println(i.next());//输出
}
}