Java Generate Sort Random Numbers
To generate random numbers and sort them in Java, you can follow these steps:
- Generate random numbers: You can use the Random class in Java to generate random numbers. Below is an example code to generate a set of random numbers:
Random random = new Random();
int[] array = new int[10]; // 生成包含10个随机数的数组
for (int i = 0; i < array.length; i++) {
array[i] = random.nextInt(100); // 生成0到99之间的随机数
}
- To sort random numbers: You can use the sort method provided by the Arrays class in Java to sort an array. Here is an example code to sort the random number array generated above:
Arrays.sort(array);
By following the above steps, random numbers can be generated and sorted. If you need to sort other types of data, you can use the sort method provided by the Collections class to sort a List collection.