方法一:
package com.dh.hrmanager.entity;
import java.util.Arrays;
import java.util.Random;
public class random {
public static void main(String[] args)
{
int []numArray=new int[30];//创建一个长度为30的数组
for(int i=0;i<numArray.length;i++)
{numArray[i]=i+1;
}//实现遍历操作
Random random=new Random();//生成随机数
int randomNum=numArray.length-1;
int []array=new int[7];
for(int i=0;i<array.length;i++) {
int index=random.nextInt(randomNum–);
array[i]=numArray[index];
int temp=numArray[index];
numArray[index]=numArray[randomNum];
numArray[randomNum]=temp;
}
Arrays.sort(array);//排序
for(int i=0;i<array.length;i++) {
System.out.println(array[i]+" ");
}
}
}
来源:
https://zhidao.baidu.com/question/809922390535914052.html