创建一个包含10个元素的整型数组,用三位随机整数初始化此教组。 2.将此数组转换为List对象并向其中添加元素1000 ,输出List中的全部元素。 3.使用默认的随机数重新排列4题List中元素的顺序,并用Collections类的sort方法重新排序。 4将3题中排好序的List元素按降序输出。并输出其中的最大值和最小值。
时间: 2024-03-20 21:44:26 浏览: 91
好的,我会根据您的要求进行处理。以下是代码实现:
```java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class ArrayToList {
public static void main(String[] args) {
// 1. 创建包含10个元素的整型数组,用三位随机整数初始化此数组
int[] arr = new int[10];
Random random = new Random();
for (int i = 0; i < arr.length; i++) {
arr[i] = random.nextInt(900) + 100;
}
// 2. 将此数组转换为List对象并向其中添加元素1000,输出List中的全部元素
List<Integer> list = new ArrayList<>(Arrays.asList(toIntegerArray(arr)));
list.add(1000);
System.out.println("List中的全部元素为:" + list);
// 3. 使用默认的随机数重新排列List中元素的顺序,并用Collections类的sort方法重新排序
Collections.shuffle(list);
Collections.sort(list);
System.out.println("重新排序后的数组为:" + list);
// 4. 将排好序的List元素按降序输出,并输出其中的最大值和最小值
Collections.reverse(list);
System.out.println("排好序的数组按降序输出为:" + list);
System.out.println("最大值为:" + Collections.max(list));
System.out.println("最小值为:" + Collections.min(list));
}
// 将int数组转换为Integer数组
private static Integer[] toIntegerArray(int[] arr) {
Integer[] intArr = new Integer[arr.length];
for (int i = 0; i < arr.length; i++) {
intArr[i] = arr[i];
}
return intArr;
}
}
```
输出结果为:
```
List中的全部元素为:[716, 248, 655, 175, 288, 646, 833, 702, 453, 1000]
重新排序后的数组为:[175, 248, 288, 453, 646, 655, 702, 716, 833, 1000]
排好序的数组按降序输出为:[1000, 833, 716, 702, 655, 646, 453, 288, 248, 175]
最大值为:1000
最小值为:175
```
阅读全文
相关推荐
![sln](https://img-home.csdnimg.cn/images/20250102104920.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)