少而好学,如日出之阳;壮而好学,如日中之光;老而好学,如炳烛之明。一一刘向

image-20201117204608973

数组转List<List<Integer>>

1
List<List<Integer>> collect = Arrays.stream(array).map(a1 -> Arrays.stream(a1).boxed().collect(Collectors.toList())).collect(Collectors.toList());

List<List<Integer>>int[][]

1
array = collect.stream().map(integers -> integers.stream().mapToInt(value -> value).toArray()).toArray(int[][]::new);

二维数组和List<List<Integer>>之间的转换使用stream的话就非常简单了

1
2
3
4
5
6
7
8
9
10
int[][] array = new int[][]{{1, 2}, {1, 3, 3, 4}, {2, 3}};
List<List<Integer>> collect = Arrays.stream(array).map(a1 -> Arrays.stream(a1).boxed().collect(Collectors.toList())).collect(Collectors.toList());
array = collect.stream().map(integers -> integers.stream().mapToInt(value -> value).toArray()).toArray(int[][]::new);

for (int[] ints : array) {
for (int anInt : ints) {
System.out.print(anInt + " ");
}
System.out.println();
}