数据容器的相互转化

旧城等待, 2023-10-04 11:45 24阅读 0赞

怎样把int数组转化为List<Integer>

不能使用Arrays.asList方法。除了直接遍历外,就是1:

  1. int[] nums = {
  2. 1,2};
  3. 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)转化为数组

以二维数组为例:

  1. List<int[]> ans = new ArrayList<>();
  2. return ans.toArray(new int[0][]);

  1. www.huaweicloud.com/articles/d5b381fa7e75b2053cfd680ce47c6ffa.html ↩︎

发表评论

表情:
评论列表 (有 0 条评论,24人围观)

还没有评论,来说两句吧...

相关阅读