int 数组转换成list
时间: 2023-09-11 10:03:14 浏览: 106
可以使用多种方法将int数组转换为List。一种方法是使用Java 8的流操作。首先,将int数组转换为数值流,然后将流中的元素装箱为Integer类型,最后将流转换为List。代码示例如下:
```java
int\[\] nums = {1, 2, 3, 4, 5};
List<Integer> list = Arrays.stream(nums)
.boxed()
.collect(Collectors.toList());
```
另一种方法是使用传统的for循环遍历int数组,并将每个元素添加到List中。代码示例如下:
```java
int\[\] nums = {1, 2, 3, 4, 5};
List<Integer> list = new ArrayList<>();
for (int element : nums) {
list.add(element);
}
```
无论使用哪种方法,最终都可以将int数组成功转换为List。\[1\]\[2\]\[3\]
#### 引用[.reference_title]
- *1* *3* [将int数组转为List集合](https://blog.csdn.net/lutrra/article/details/123101123)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [java int数组如何转换为List集合](https://blog.csdn.net/weixin_44712870/article/details/123910160)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文