简体   繁体   中英

Java 8 way to convert int[][] to Integer[][]

How to convert java int[][] to Integer[][] ? .It seems easy to convert single dimensional primitive array to Object type single dimensional array using java stream.

For example

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

Is there any way for two dimensional array like above ?

You can do the same thing within a map operation

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

You can use Arrays.stream method:

int[][] arr1 = {};

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM