当前位置:网站首页>记录一次数组转集合出现错误的坑点,尽量使用包装类型数组进行转换
记录一次数组转集合出现错误的坑点,尽量使用包装类型数组进行转换
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);
}
}
运行结果

边栏推荐
- Debian侵犯Rust商标,妥协改名还是会得到豁免?
- 期货开户调整交易所保证金标准
- Newton's theorem and related corollaries
- 22.卷积神经网络实战-Lenet5
- C语言实验八 字符数组程序设计
- ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
- TKU记一次单点QPS优化(顺祝ITEYE终于回来了)
- 3 Month Tester Readme: 4 Important Skills That Impacted My Career
- hutool工具-----JSON工具-JSONUtil
- datagrip 报错 “The specified database userpassword combination is rejected...”的解决方法
猜你喜欢
随机推荐
5年自动化测试经验的一些感悟:做UI自动化一定要跨过这10个坑
电商库存系统的防超卖和高并发扣减方案
C语言实验十 函数(二)
管理基础知识18
管理基础知识16
GateWay实现负载均衡
Day.js 常用方法
PowerBI商学院佐罗BI真经连续剧
IDEA找不到Database解决方法
浅谈国产ERP的“横纵竖”三向发展态势
TKU记一次单点QPS优化(顺祝ITEYE终于回来了)
hutool工具-----JSON工具-JSONUtil
Interview: Briefly describe a project you are involved in
Local storage in Kubernetes
Markdown (CSDN) MD编辑器(四)- 漂亮表格(表格背景色、跨行、跨列)
大话西游创建角色失败解决
22.卷积神经网络实战-Lenet5
Image fusion based on weighted 】 and pyramid image fusion with matlab code
期货开户调整交易所保证金标准
Kubernetes — Flannel









