【问题标题】:Java 8 way to convert int[][] to Integer[][]Java 8 将 int[][] 转换为 Integer[][] 的方法
【发布时间】:2020-06-14 20:24:14
【问题描述】:

如何将java int[][] 转换为Integer[][] ? .使用java流将一维原始数组转换为Object类型的一维数组似乎很容易。

例如

Integer[] result = IntStream.of( intarray ).boxed().toArray( Integer[]::new );

上面的二维数组有什么办法吗?

【问题讨论】:

    标签: java arrays matrix multidimensional-array java-8


    【解决方案1】:

    您可以在 map 操作中执行相同的操作

    int[][] twoDimenArray = {};
    Integer[][] result = Stream.of(twoDimenArray)
        .map(array -> IntStream.of(array).boxed().toArray(Integer[]::new))
        .toArray(Integer[][]::new);
    

    【讨论】:

      【解决方案2】:

      你可以使用Arrays.stream方法:

      int[][] arr1 = {};
      
      Integer[][] arr2 = Arrays.stream(arr1)
              .map(row -> Arrays.stream(row)
                      .boxed()
                      .toArray(Integer[]::new))
              .toArray(Integer[][]::new);
      

      【讨论】:

        猜你喜欢
        • 2021-07-16
        • 1970-01-01
        • 1970-01-01
        • 2016-01-21
        • 2011-10-01
        • 2020-07-06
        • 1970-01-01
        • 1970-01-01
        • 2010-10-27
        相关资源
        最近更新 更多