日期:2014-05-20 浏览次数:20841 次
import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.Random; public class HashSetDemo { public static void main(String[] args) { ArrayList<Integer> al=new ArrayList<Integer>(); for (int i = 0; i < 54; i++) { al.add(i); } HashSet<Integer> hs=new HashSet<Integer>(); Random rd=new Random(); for (int i = 0; i < 54;) { int k=rd.nextInt(54); if(hs.contains(al.get(k))){ continue; } hs.add(al.get(k)); i++; } Iterator<Integer> it=hs.iterator(); while(it.hasNext()){ System.out.println(it.next()); } } }