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

边栏推荐
猜你喜欢
随机推荐
管理基础知识16
Day.js 常用方法
Day11 Shell scripting basics
Entry name ‘org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt’ collided
期货公司开户实力经纪业务的规模
交返是做日内交易的必要条件
C语言实验七 二维数组程序设计
Some insights from 5 years of automated testing experience: UI automation must overcome these 10 pits
PowerBI商学院佐罗BI真经连续剧
Rust P2P网络应用实战-1 P2P网络核心概念及Ping程序
C语言实验八 字符数组程序设计
Can‘t connect to MySQL server on ‘localhost3306‘ (10061) 简洁明了的解决方法
pcie inbound和outbound关系
百度、百图生科 | HelixFold-Single: 使用蛋白质语言模型作为替代进行无MSA蛋白质结构预测
IDEA找不到Database解决方法
For effective automated testing, these software testing tools must be collected!!!
JDBC PreparedStatement 的命名参数实现
传统企业数字化转型需要经过几个阶段?
微信支付软件架构,这也太牛逼了!
信息化和数字化的本质区别是什么?









