我试图在递归方法中将项添加到两个单独的列表中。因为我需要结果是两个单独的列表(或者可能是地图)。
我希望输出为两个列表,其中根据最便宜的成本列出需要分配的所有任务的输入。所以:
Set {task1, task2, task3, task 4, etc.}
A: [task2, task3]
B: [task1, task4]
代码如下所示:
public Assignments {
Set<Task> unassignedTasks = new HashSet<>();
List<Task> assignment = new ArrayList<>();
List<Task> firstTasks = [task0, task00] //a list with the task that is already being performed for two assignees
public Assignment assign(Task task1, Assignments sched){
//fields to store some attributes of task1
/* recursive function */
for(Task task2: unassignedTasks){
// fields to store some attributes of task2
if(task2 fits after task1){
// calculate cost of performing task2 at this point in time
double cost = task1.getCost() + assign(task2, sched));
if(cost < value){
assignment.add(task1);
}
} else if(!firstTasks.isEmpty()){
// here I'd like it to skip to the second list
firstTasks.remove(0);
Task nextCurrentTask = firstTasks.get(0);
cost = cost + assign(nextCurrentTask, sched);
}
}
}
}
显然,这种方法导致一个列表的输出。 我的问题是,如果算法最终在else-if块中迭代到第二个列表并构造此列表,我怎么能确保?
我想到了列表List<List<Object>>,
,但后来我遇到了在正确的时间将初始列表添加到列表列表的问题。有什么想法吗?