package cn.itcast_1;
import java.util.TreeSet;
public class Demo {
public static void main(String[] args) {
int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
TreeSet<Integer> ts = new TreeSet<Integer>();
while (ts.size() < 5) {
int i = (int) (Math.random() * 10);
if (i < arr.length) {
ts.add(arr[i]);
}
}
System.out.println(ts);
}
}
试试看行不行
要取出来的话遍历集合就好了 |