/* * 需求:隨機獲取10個1-20之間的隨機數,輸出不重復的不能重復 */ public class Demo { public static void main(String[] args) { // 創建大集合 ArrayList<Integer> array = new ArrayList<>(); //產生10個10到20的隨機數 for(int x=0;x<10;x++){ int y =(int) (Math.random()*10+10); //輸出產生的數 System.out.print(y+"\t"); //判斷沒有就添加 if(!array.contains(y)){ array.add(y); } } System.out.println(); Iterator<Integer> it = array.iterator(); while(it.hasNext()){ int a =it.next(); System.out.print(a+"\t"); } } }
