当前位置:网站首页>记录一次数组转集合出现错误的坑点,尽量使用包装类型数组进行转换
记录一次数组转集合出现错误的坑点,尽量使用包装类型数组进行转换
2022-08-02 01:06:00 【百思不得小赵】
使用数组转集合的时候尽量使用包装类型数组进行转换。
描述
- 使用基本数据类型去创建数组的时候,当想将其转为集合的时候,相当于集合中存放的是一个引用。
- 使用包装类型创建数组,将其转换为集合时,集合中存放的是数组中的值
数组转集合的方式
Arrays.asList(数组);转换后的集合不可以进行添加或删除等修改操作,否则会报错Collections.addAll(arrayList, strArray)Arrays.stream(ints).boxed().collect(Collectors.toList());
代码示例
public class Test {
public static void main(String[] args) {
int[] arrInt = {
1, 2, 3, 2, 2, 3, 2, 3};
List<int[]> ints = Arrays.asList(arrInt);
System.out.println(ints);
Integer[] arrInteger = {
1, 2, 3, 2, 2, 3, 2, 3};
List<Integer> integers = Arrays.asList(arrInteger);
System.out.println(integers);
}
}
运行结果

边栏推荐
猜你喜欢

flv.js解析与使用

哈希表

Some insights from 5 years of automated testing experience: UI automation must overcome these 10 pits

pcie inbound和outbound关系

字节给我狠狠上了一课:危机来的时候你连准备时间都没有...

浅谈国产ERP的“横纵竖”三向发展态势

Why is on-chain governance so important, and how will Polkadot Gov 2.0 lead the development of on-chain governance?

Reflex WMS中阶系列7:已经完成拣货尚未Load的HD如果要取消拣货,该如何处理?

datagrip连接mysql数据库

Reflex WMS中阶系列6:对一个装货重复run pick会有什么后果?
随机推荐
Debian侵犯Rust商标,妥协改名还是会得到豁免?
管理基础知识13
去经营企业吧
技术分享 | 接口测试中如何使用Json 来进行数据交互 ?
Some insights from 5 years of automated testing experience: UI automation must overcome these 10 pits
flyway的快速入门教程
安全(2)
Oracle data to mysql FlinkSQL CDC to achieve synchronization
信息收集之cms指纹识别
Pcie the inbound and outbound
IDEA找不到Database解决方法
When paying attention to the "Internet +" model, you usually only focus on the "Internet +" model itself
滴滴秋招提前批正式开始,现在投递免笔试
"Introduction to Natural Language Processing Practice" Question Answering Robot Based on Knowledge Graph
MySQL——增删查改操作
Day.js 常用方法
Kubernetes之本地存储
iframe使用
Image fusion based on weighted 】 and pyramid image fusion with matlab code
外包干了三年,废了...