如何通过以循环方式合并 3 个数组列表来创建新列表?



我有 3 个数组。我想合并 3 个数组并创建一个显示所有合并整数的新数组。我希望这个新数组采用循环风格。

示例输入:

array = {arr1 = 1, 2, 3, arr2 = 4, 5, 6, arr3 = 7, 8, 9}

示例输出:

arr4 = 1, 4, 7, 2, 5, 8, 3, 6, 9

我应该使用这个函数,但我不明白如何使用listOfLists

String roundRobin(List<List<Integer>>listOfLists) {

你可以更简单地使用ArrayLists或Arrays来做到这一点。

使用数组,您可以创建 3 个int[]和 1 个int[][]。 这转化为"3 个ints 数组和 1 个ints数组"。这就是#4:其他数组的数组。在代码中,它是:

int[]
arr1 = {1, 2, 3},
arr2 = {4, 5, 6},
arr3 = {7, 8, 9};
int[][] arr4 = {arr1, arr2, arr3};

或者,您可以使用ArrayLists,它与 Arrays 的不同之处在于您可以添加或删除元素以及许多其他操作。在这种情况下,逻辑是相同的,只是语法不同。你创建 3 个ArrayList<Integer>和 1 个ArrayList<ArrayList<Integer>>,翻译为"3ArrayList秒的Integer秒和 1ArrayList秒的ArrayList秒的Integer秒"。 在代码中它是:

ArrayList<Integer>
list1 = new ArrayList<>(Arrays.asList(1, 2, 3)),
list2 = new ArrayList<>(Arrays.asList(4, 5, 6)),
list3 = new ArrayList<>(Arrays.asList(7, 8, 9));
List<ArrayList<Integer>> list4 = new ArrayList<>
(Arrays.asList(list1, list2, list3));

最后,您可以打印两种方法的输出:

System.out.println("Arrays - int[][]: " + Arrays.deepToString(arr4)
+ "nLists - List<ArrayList<Integer>>: " + list4);

您将获得:

Arrays - int[][]: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Lists - List<List<Integer>>: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

这有帮助吗?

您可以使用两个嵌套的 for 循环:

List<List<Integer>> lists = List.of(
List.of(1, 2),
List.of(3, 4, 5, 6),
List.of(7, 8, 9));
List<Integer> listRobin = new ArrayList<>();
// infinite loop through the columns
for (int i = 0; ; i++) {
// if the maximum number
// of columns is reached
boolean max = true;
// loop through the rows, aka inner lists
for (List<Integer> row : lists) {
// if this column is present
if (i < row.size()) {
// column is present
max = false;
// take value from the column
listRobin.add(row.get(i));
}
}
// while the columns are still present
if (max) break;
}
// output
System.out.println(listRobin);
// [1, 3, 7, 2, 4, 8, 5, 9, 6]

另请参阅:先按列填充交错的二维数组

我认为只需循环输入并获取元素并将元素添加到数组列表中就很容易理解。

public static List<Integer> roundRobin(List<List<Integer>> listOfLists) {
if (listOfLists == null || listOfLists.isEmpty()) {
return new ArrayList<>();
}
int maxLength = -1;
for (List<Integer> list : listOfLists) {
if (list.size() > maxLength) {
maxLength = list.size();
}
}
List<Integer> result = new ArrayList<>(maxLength * listOfLists.size());
for (int i = 0; i < maxLength; i++) {
for (List<Integer> list : listOfLists) {
if (i < list.size()) {
result.add(list.get(i));
}
}
}
return result;
}

若要从二维列表的列填充一维列表,可以使用两个嵌套流:首先按列,然后按行。内部列表的长度无关紧要,在外部流中,您可以在列仍然存在时遍历。

List<List<Integer>> listOfLists = List.of(
List.of(1, 2, 3, 4),
List.of(5, 6),
List.of(7, 8, 9));
List<Integer> listRobin = IntStream
// infinite Stream through
// the columns of a 2d list
.iterate(0, i -> i + 1)
// take the values from the column
// Stream<List<Integer>>
.mapToObj(i -> listOfLists
// iterate over the inner lists
.stream()
// take those lists where
// this column is present
.filter(list -> list.size() > i)
// take value from the column
.map(list -> list.get(i))
// return a new list
.collect(Collectors.toList()))
// while the columns are still present
.takeWhile(list -> list.size() > 0)
// flatten to a single stream
// Stream<Integer>
.flatMap(List::stream)
// return a new list
.collect(Collectors.toList());
// output
System.out.print(listRobin); // [1, 5, 7, 2, 6, 8, 3, 9, 4]

另请参阅:使用循环算法从多个列表中选择数据的有效方法

最新更新