vault backup: 2026-11-07 16:33:44

This commit is contained in:
cheney
2026-11-07 16:33:55 +08:00
commit 7015312702
1208 changed files with 132432 additions and 0 deletions
@@ -0,0 +1,112 @@
###### 1. Integer[]转[ArrayList](https://so.csdn.net/so/search?q=ArrayList&spm=1001.2101.3001.7020)
(1) 方法一:
利用[Arrays工具类](https://blog.csdn.net/k909397116/article/details/107308897)中的`asList`方法
```Java
Integer[] arr = {1,2,3};
ArrayList<Integer> list = new ArrayList<>(Arrays.asList(arr));
12
12
```
(2) 方法二:
利用Collections工具类中的`addAll`方法
```Java
Integer[] arr = {1,2,3};
ArrayList<Integer> list = new ArrayList<>(array.length);
Collections.addAll(list, arr);
123
123
```
(3) 注意:
Java中集合只能存放引用数据类型,在使用`asList`或`addAll`方法时,被转换的[数组](https://so.csdn.net/so/search?q=%E6%95%B0%E7%BB%84&spm=1001.2101.3001.7020)必须是存放引用数据类型的数组,如果是基本数据类型数组请在转换前先把其转换为对应的[包装类型](https://blog.csdn.net/k909397116/article/details/107323121)数组,下面会介绍。
###### 2. ArrayList转Integer[]
```Java
ArrayList<Integer> list = new ArrayList<>();
Integer[] arr = list.toArray(new Integer[0]);
12
12
```
## 二、Integer[]与int[]互转
---
###### 1. Integer[]转int[]
```Java
Integer[] arr1 = {1,2,3};
int[] arr2 = Arrays.stream(arr1).mapToInt(Integer::valueOf).toArray();
12
12
```
###### 2. int[]转Integer[]
```Java
int[] arr1 = {1,2,3};
Integer[] arr2 = Arrays.stream(arr1).boxed().toArray(Integer[]::new);
12
12
```
## 三、int[]与ArrayList的互转
---
###### 1. int[]转ArrayList
```Java
int[] arr = {1,2,3};
List<Integer> list = Arrays.stream(arr).boxed().collect(Collectors.toList());
12
12
```
###### 2. ArrayList转int[]
```Java
ArrayList<Integer> list = new ArrayList<>();
int[] arr = list.stream().mapToInt(Integer::valueOf).toArray();
```