如何简便的将List<Integer>转换成int[]?

使用Java 8的流(Streams)

java 复制代码
ArrayList<Integer> list = new ArrayList<>();
int[] intArray = list.stream().mapToInt(Integer::intValue).toArray();

若是maven项目可使用Apache Commons Lang库

XML 复制代码
    <dependency>  
        <groupId>org.apache.commons</groupId>  
        <artifactId>commons-lang3</artifactId>  
        <version>3.12.0</version> <!-- 请检查是否有更新的版本 -->  
    </dependency>
java 复制代码
ArrayList<Integer> list = new ArrayList<>(); 
Integer[] integerArray = list.toArray(new Integer[0]);  
int[] intArray = ArrayUtils.toPrimitive(integerArray);
相关推荐
careathers4 小时前
【数据结构】从堆到 PriorityQueue:手写一个优先级队列
数据结构
y1su5 小时前
Leetcode 字符串模板
java·数据结构·算法·leetcode·职场和发展·动态规划
十二11 小时前
【洛谷题解】P1115 最大子段和(前缀和 + 维护最小前缀)
数据结构·c++·算法·蓝桥杯
2501_9339232512 小时前
双指针算法:移动零+复写零(Java)
数据结构·算法·双指针
suhan15 小时前
【数据结构】六、图:4.图的遍历(深度优先算法DFS、广度优先算法BFS)
数据结构·算法·排序算法
suhan15 小时前
【数据结构】六、图:3.十字链表、邻接多重表、边集数组
数据结构·算法·排序算法
kiracrimson16 小时前
STL简单引入:把数据结构做成模板
开发语言·数据结构·c++
HPtiger16 小时前
交换类排序详解:冒泡排序与快速排序
数据结构·算法·排序算法
kyle~16 小时前
数据结构 --- 无锁队列(Lock-Free Queue)
linux·数据结构·c++·多线程·并发
Cccp.12317 小时前
【leetcode】(十四)从暴力递归到动态规划
数据结构·算法·leetcode·动态规划