标签:code oid 随机 stat 数组 获取 com bsp integer
package com.data.manger.follow.controller; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Random; /** * 获取随机数,且不重复. */ public class Test{ public static void main(String[] args) { //定义随机取的位数(范围判断) int n = Integer.parseInt("0000".replace("0", "9")); //定义随机生成的数据 int[] randomDataCode = new int[n]; for (int i = 0; i < n; i++) { randomDataCode[i] = i + 1; } System.out.println("默认顺序生成的数组总量 ====== " + Arrays.toString(randomDataCode)); Random r = new Random(); for (int i = 0; i < n; i++) { int in = r.nextInt(n - i) + i; int t = randomDataCode[in]; randomDataCode[in] = randomDataCode[i]; randomDataCode[i] = t; } System.out.println("打乱后的数组 ====== " + Arrays.toString(randomDataCode)); //数组保存到list List<Integer> randomDataCodeId = new ArrayList<Integer>(randomDataCode.length);//随机取的所有数值 for(Integer uid: randomDataCode){ randomDataCodeId.add(uid); } //判断随机数是否存在 List<Integer> dataList = new ArrayList<>();//已经存在的数值 dataList.add(1111); dataList.add(2222); for (Object data : dataList) { for (Integer list : randomDataCodeId) { if (list == data) { randomDataCodeId.remove(list); } } } System.out.println("随机取到的数为 ====="+ randomDataCodeId.get(0)); } }
标签:code oid 随机 stat 数组 获取 com bsp integer
原文地址:https://www.cnblogs.com/zengsiqi/p/13175910.html