ArrayList<Integer>如何转换为int[]数组
来源:互联网 发布:log4j写入mysql数据库 编辑:程序博客网 时间:2024/05/22 00:17
网上搜Arraylist和数组互相转换的方法时,举的例子都是String类型的。比如:
但是对于int类型如果这样写:
ArrayList<Integer> a=new ArrayList<Integer>();int[] array=(int[])a.toArray(new int[size]);//会报错则会报错,这是因为int[]并不等同于Integer[]。因此如果换成Integer[]数组,则能正确运行。
List<Integer> list = new ArrayList<Integer>();list.add(1);list.add(2);Integer[] array = list.toArray(new Integer[list.size()]);//能正确运行for(int element:array){ System.out.println(element);}
如果非得希望得到int[]的话,只能用循环赋值来得到了。
int[] d = new int[list.size()];for(int i = 0;i<list.size();i++){ d[i] = list.get(i);}如果既不想用循环,又想要得到int[],那就只能在jdk8中使用IntStream了。
阅读全文
0 0
- ArrayList<Integer>如何转换为int[]数组
- ArrayList<Integer>如何转换为数组
- Java8<将Integer[]转换为int[]数组>
- ArrayList转换为数组
- Java如何将数组转换为ArrayList(ArrayList和数组间的相互转换)
- arraylist转换为数组 ,数组转换为arraylist
- 关于ArrayList数组转换为Integer类型的报错问题
- .net ArrayLIst 转换为数组
- 转换数组 Array为ArrayList
- 将Arraylist转换成int数组
- ArrayList<Object> 转换为Map<Integer, ArrayList<String>>
- char数组如何强制转换为int类型
- java中如何将字符串转换为int数组
- int转换为byte数组
- int数组转换为字符串
- ArrayList<T>数组转换为二维数组
- c# string[] 数组 转换为 int[]数组
- c# int[]数组转换为object[]数组
- 抽象代数学习笔记(1) 集合
- Fortran-IMSL的下载与安装
- 51nod 1551 集合交易
- Linux操作系统部分指令
- Letter Combinations of a Phone Number(回溯,dfs)
- ArrayList<Integer>如何转换为int[]数组
- [R语言]蒙特卡罗模拟检验CLRM假定下最小二乘量的BLUE性质
- Spring 中 @Controller 和 @RestController
- 【算法】程序猿不写代码是不对的75
- Spark Python 快速体验
- 12th 【基础】二进制计数
- 走进官方手册系列 --- 详解InnoDB针对不同类型的SQL所采取的锁策略
- 反向传播BP算法
- Mac下写bitcoin执行脚步