兩個list 合併後去除掉重複removeAll()的,然後再隨機獲取最後list中的 幾個值
阿新 • • 發佈:2018-12-27
public static void test1(){
String s1="1,2,5,3,6,9";
String n1[]=s1.split(",");
System.out.println("n1length="+n1.length);
String s2="2,3";
String n2[]=s2.split(",");
System.out.println("n2length="+n2.length);
List<String> list1=new ArrayList<>();
for(int i=0;i<n1.length;i++){
list1.add(n1[i]);
}
List<String> list2=new ArrayList<>();
for(int i=0;i<n2.length;i++){
list2.add(n2[i]);
}
System.out.println("list1="+list1);
System.out.println("list2="+list2);
list1.addAll(list2);
List<String> list=new ArrayList<String>();
list=list1;
list1.removeAll(list2);
System.out.println("list="+list );
int number=2;
List<String> result=new ArrayList<>();
for(int i = 0;i<number;i++){
//顯示數字並將其從列表中刪除,從而實現不重複.
int random=new Random().nextInt(list.size());
System.out.println("第"+i+"隨機數:"+random);
result.add(list.get(random));
System.out.println(list.remove(random));
}
System.out.println("結果:"+result);
}