gpt4 book ai didi

java - 根据指定顺序添加列表的更有效方法

转载 作者:行者123 更新时间:2023-12-02 03:43:00 24 4
gpt4 key购买 nike

我有三个不同的 ArrayList 需要合并为一个,但是,它们在堆栈中的顺序很重要并且是指定的。我怎样才能更有效地做到这一点?

代码所做的是将 ArrayList 添加到完整 ArrayList 的适当级别(0,1,2),但是有太多重复代码,因此该方法看起来很糟糕。

最终目标如下——我们关心的列与所需的顺序一起传递给我们的函数。然后,根据顺序,我们将其放在适当的位置,其他两列填充其余部分(它们的顺序无关)。

所以基本上,Column3 被传递,例如,顺序为 2。这意味着我们希望完整的 ArrayList 是 Column1 的内容,然后是 Column3 的内容,然后是 Column2 的内容(其中 Column1 和 Column2 可以交换,因为它们的顺序实际上并不相同)事)。

谢谢。

public void assemble(List<Card> selection, int order) {

switch(order) {
case 0:
trickDeck.addAll(selection.stream().collect(Collectors.toList()));
if (selection == column1) {
trickDeck.addAll(column2.stream().collect(Collectors.toList()));
trickDeck.addAll(column3.stream().collect(Collectors.toList()));
}
else if (selection == column2) {
trickDeck.addAll(column1.stream().collect(Collectors.toList()));
trickDeck.addAll(column3.stream().collect(Collectors.toList()));
}
else {
trickDeck.addAll(column1.stream().collect(Collectors.toList()));
trickDeck.addAll(column2.stream().collect(Collectors.toList()));
}
break;
case 1:
if (selection == column1) {
trickDeck.addAll(column2.stream().collect(Collectors.toList()));
trickDeck.addAll(selection.stream().collect(Collectors.toList()));
trickDeck.addAll(column3.stream().collect(Collectors.toList()));
}
else if (selection == column2) {
trickDeck.addAll(column1.stream().collect(Collectors.toList()));
trickDeck.addAll(selection.stream().collect(Collectors.toList()));
trickDeck.addAll(column3.stream().collect(Collectors.toList()));
}
else {
trickDeck.addAll(column1.stream().collect(Collectors.toList()));
trickDeck.addAll(selection.stream().collect(Collectors.toList()));
trickDeck.addAll(column2.stream().collect(Collectors.toList()));
}
break;
case 2:
if (selection == column1) {
trickDeck.addAll(column2.stream().collect(Collectors.toList()));
trickDeck.addAll(column3.stream().collect(Collectors.toList()));
trickDeck.addAll(selection.stream().collect(Collectors.toList()));
}
else if (selection == column2) {
trickDeck.addAll(column1.stream().collect(Collectors.toList()));
trickDeck.addAll(column3.stream().collect(Collectors.toList()));
trickDeck.addAll(selection.stream().collect(Collectors.toList()));
}
else {
trickDeck.addAll(column1.stream().collect(Collectors.toList()));
trickDeck.addAll(column2.stream().collect(Collectors.toList()));
trickDeck.addAll(selection.stream().collect(Collectors.toList()));
}
break;
default:
break;
}
}

最佳答案

我建议使用列索引而不是传入实际的List。然后我们可以将列作为数组进行操作。这是未经测试的演示:

public void assemble(int selection, int position) {
// create list of columns
List<List<Card>> columns = Arrays.asList(column1, column2, column3);

// if selected column is not located at position, swap:
if (selection != position) {
List<Card> temp = columns.get(position);
columns.set(position, columns.get(selection));
columns.set(selection, temp);
}

// add all columns to trickDeck in order
columns.forEach(trickDeck::addAll);
}

关于java - 根据指定顺序添加列表的更有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36636455/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com