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

边栏推荐
猜你喜欢

如何期货开户和选择期货公司?

kubernetes之服务发现

datagrip 报错 “The specified database userpassword combination is rejected...”的解决方法

Image fusion based on weighted 】 and pyramid image fusion with matlab code

力扣 1374. 生成每种字符都是奇数个的字符串

H5页面调用微信授权获取code

ERROR 1819 (HY000) Your password does not satisfy the current policy requirements

html+css+php+mysql实现注册+登录+修改密码(附完整代码)

6-24漏洞利用-vnc密码破解

Pytorch seq2seq model architecture to achieve English translation tasks
随机推荐
C语言:打印整数二进制的奇数位和偶数位
Mapped Statements collection does not contain value for的解决方法
JDBC PreparedStatement 的命名参数实现
Kubernetes之本地存储
ofstream,ifstream,fstream读写文件
C语言实验六 一维数组程序设计
简单工厂模式
当关注「互联网+」模式的时候,通常仅仅只是在关注「互联网+」模式本身
go泛型使用方法
Detailed explanation of fastjson
ImportError cannot import name ‘Mapping‘ from ‘collections‘
Kubernetes — 网络流量模型
60种特征工程操作:使用自定义聚合函数【收藏】
Test Cases: Four-Step Test Design Approach
牛顿定理和相关推论
【软件工程之美 - 专栏笔记】34 | 账号密码泄露成灾,应该怎样预防?
Kubernetes — 核心资源对象 — 网络
H5页面调用微信授权获取code
flask获取post请求参数
管理基础知识12