package exercise;
import java.util.HashSet;
import java.util.Random;
public class TreeSet {
public static void main(String[] args) {
// 创建随机数对象
Random r = new Random();
// 创建一个set集合
HashSet<Integer> ts = new HashSet<Integer>();
// 判断集合的长度是不是小于10
while (ts.size() < 10) {
int num = r.nextInt(20) + 1;
ts.add(num);
}
// 遍历
for (Integer i : ts) {
System.out.println(i);
}
import java.util.HashSet;
import java.util.Random;
public class TreeSet {
public static void main(String[] args) {
// 创建随机数对象
Random r = new Random();
// 创建一个set集合
HashSet<Integer> ts = new HashSet<Integer>();
// 判断集合的长度是不是小于10
while (ts.size() < 10) {
int num = r.nextInt(20) + 1;
ts.add(num);
}
// 遍历
for (Integer i : ts) {
System.out.println(i);
}
}
运行结果:
}