如何将 2 个 arraylist 组合成 java 中的列表列表
How do I combine 2 arraylist into a list of lists in java
我想将 2 个数组列表转换为数组的数组列表
newList3 = [-50, 30, -20, 0, 20, -30, 50]
newList4 = [1, 1, 1, 1, 1, 1, 1]
我想return:
[[-50, 1], [30, 1], [-20, 1], [0, 1], [20, 1], [-30, 1], [50, 1]]
我能得到的唯一结果是:
[-50, 1, 30, 1, -20, 1, 0, 1, 20, 1, -30, 1, 50, 1]
我试过了
a = newList3.get(0);
b = newList4.get(0);
newList.add(a);
newList.add(b);
newList.add(newList2);
newList.clear();
a = newList3.get(1);
b = newList4.get(1);
newList.add(a);
newList.add(b);
newList.add(newList);
您要查找的操作称为压缩操作。
IntStream
.range(0, Math.min(list1.size(), list2.size()))
.mapToObj(i -> Arrays.asList(list1.get(i), list2.get(i)))
.collect(Collectors.toList());
在这里,由于我们将遍历我们需要索引的列表。因此,我们使用 IntStream.range
来生成索引范围。然后我们使用 mapToObj
压缩 2 个列表。
在范围内,我们将从 0
到具有最少元素的列表大小。
我想将 2 个数组列表转换为数组的数组列表
newList3 = [-50, 30, -20, 0, 20, -30, 50]
newList4 = [1, 1, 1, 1, 1, 1, 1]
我想return: [[-50, 1], [30, 1], [-20, 1], [0, 1], [20, 1], [-30, 1], [50, 1]]
我能得到的唯一结果是: [-50, 1, 30, 1, -20, 1, 0, 1, 20, 1, -30, 1, 50, 1]
我试过了
a = newList3.get(0);
b = newList4.get(0);
newList.add(a);
newList.add(b);
newList.add(newList2);
newList.clear();
a = newList3.get(1);
b = newList4.get(1);
newList.add(a);
newList.add(b);
newList.add(newList);
您要查找的操作称为压缩操作。
IntStream
.range(0, Math.min(list1.size(), list2.size()))
.mapToObj(i -> Arrays.asList(list1.get(i), list2.get(i)))
.collect(Collectors.toList());
在这里,由于我们将遍历我们需要索引的列表。因此,我们使用 IntStream.range
来生成索引范围。然后我们使用 mapToObj
压缩 2 个列表。
在范围内,我们将从 0
到具有最少元素的列表大小。