数据容器的相互转化
怎样把int数组转化为List<Integer>
不能使用Arrays.asList方法。除了直接遍历外,就是1:
int[] nums = {
1,2};
List<Integer> numCandidates = Arrays.stream(nums).boxed().collect(Collectors.toList());
另外,Arrays.asList方法还有一大坑爹的是会”Returns a fixed-size list backed by the specified array”
怎样把Collection(List,Set)转化为数组
以二维数组为例:
List<int[]> ans = new ArrayList<>();
return ans.toArray(new int[0][]);
- www.huaweicloud.com/articles/d5b381fa7e75b2053cfd680ce47c6ffa.html ↩︎
还没有评论,来说两句吧...