给定一个int[] [{7, 0}, {7, 1}, {6, 1}, {5, 0}, {5, 2}, {4, 4}]列表,我需要使用Java8将其转换为2D数组{{7, 0}, {7, 1}, {6, 1}, {5, 0}, {5, 2}, {4, 4}}。
在Java8之前,我们可以使用以下逻辑:temp是List<int[]>,它包含上面的元素列表。首先,创建res[][]的大小与temp中的元素列表相同。
int[][] res = new int[temp.size()][2];
for (int i = 0; i < temp.size(); i++) {
res[i][0] = temp.get(i)[0];
res[i][1] = temp.get(i)[1];
}发布于 2020-07-06 02:29:28
尝尝这个。
List<int[]> list = List.of(
new int[] {7, 0}, new int[] {7, 1},
new int[] {6, 1}, new int[] {5, 0},
new int[] {5, 2}, new int[] {4, 4});
int[][] res = list.stream().toArray(int[][]::new);
System.out.println(Arrays.deepToString(res));结果
[[7, 0], [7, 1], [6, 1], [5, 0], [5, 2], [4, 4]]https://stackoverflow.com/questions/62748292
复制相似问题