泛型数组List和数组转换常见方法归纳 + 流方法解析

本文详细总结了Java中泛型数组与引用类型、基本类型数组之间的转换方法,包括List转引用类型数组、引用类型数组转List,以及基本类型数组的转换。同时,介绍了使用流进行转换的操作,强调了装箱和拆箱在转换过程中的作用,提供了一种理解和记忆转换方法的方式。
摘要由CSDN通过智能技术生成

本文为归纳总结,不会具体说明用法或案例,但会附上相关链接
此外,迭代可以完成任意操作,但没什么好讲的。
所以解决方法并没有写迭代

分两类:
泛型数组(List为例)和

  1. 引用类型数组转换
  2. 基本类型数组转换

前置知识:泛型必须是引用类型,不能是基本类型

方法大总结

  1. List 转 引用类型 : toArray(指定参数)

    List<int[]> list = new LinkedList<>();
    int[][] ans  = list.toArray  (new int[list.size()][]);
    
  2. 引用类型 转 List : Collections.addAll(des, src) (Arrays.asList 不好用)

    String[] strArray = {
           "array-a", "array-b" };
    List<String> strList = new ArrayList<>();
    Collections.addAll(strList, strArray);
    
  3. 基本类型 转 List / 包装

    int[] intArray = {
          1, 2, 3, 4};
    List<Integer> list = new ArrayList<>();
    list = Arrays.stream(intArray)
                 .boxed()
        		.collect(Collectors.toList());
    // 最后的collect换成: 
    //.toArray(Integer[]::new)  转为Integer[]
    
  4. List / 包装 转 基本类型

    List<Integer> list = new ArrayList<>();
    //原数组为包装Integer[] integers 第一行换为 : Arrays.stream(integers) 
    int[] intArray = list.stream()
       				    .mapToInt<
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值