private static void method() { ArrayList<Integer> nums = getArray(COUNT); ArrayList<Integer> newNums = new ArrayList<Integer>(); for (int i = COUNT; i > 0; i--) { int num = (int) (Math.random() * i); newNums.add(nums.remove(num)); } System.out.println(newNums); } public static ArrayList<Integer> getArray(int num) { ArrayList<Integer> nums = new ArrayList<Integer>(); for (int i = 0; i < num; i++) { nums.add(i + 1); } return nums; } public static final int COUNT = 9;
- (NSArray *)createNumberArray{ NSMutableArray *array = [NSMutableArray array]; for (NSInteger i = 1; i < 10; i++) { [array addObject:@(i)]; } for (NSInteger i = 0; i < 9; i++) { NSInteger seed = i + arc4random_uniform(9 - i); [array exchangeObjectAtIndex:i withObjectAtIndex:seed]; } return array; }