日期:2014-05-20 浏览次数:20617 次
Set<Integer> set = new HashSet<Integer>(); while (set.size() != 6) { int randomValue = random.nextInt(33) + 1; set.add(randomValue); }
import java.util.Arrays; import java.util.HashSet; import java.util.Iterator; import java.util.Random; import java.util.Set; public class Test { public static void main(String[] args) { int[] arr = new int[] { 1, 2, 3, 4, 5, 6 }; Set<Integer> set = new HashSet<Integer>(); Random random = new Random(); while (set.size() != 6) { int randomValue = random.nextInt(33) + 1; set.add(randomValue); } System.out.println(check(arr, set)); } static boolean check(int[] arr, Set<Integer> set) { if(arr.length != set.size()) return false; Iterator<Integer> iterator = set.iterator(); boolean flag = true; int[] result = new int[arr.length]; while(iterator.hasNext()){ Integer n = iterator.next(); for(int i = 0;i < arr.length;i++){ if(n == arr[i] && result[i] == 0){ result[i] = 1; break; } } } for(int i = 0;i < result.length && flag;i++) flag &= (result[i] == 1); return flag; } }