I want to implement a recursive function, which iterates through a sublist of an object within a list and add all of the elements in the sublists into a grand list.
My object is as follows:
List<Task> tasks with a function called .getDependsOn() which returns a List<Task> of which it depends.
From the beginning of a Task, which depends on Task B, which then again depends on Task C and D, I want to recursively iterate through every Task and add them to a big list, which includes EVERY dependent tasks of Task A.
public List<Task> addDependencies(List<Task> tasks) {
for (Task task: tasks) {
if (!task.getDependsOn().isEmpty()) {
tasks.addAll(addDependencies(task.getDependsOn()));
return tasks;
} else {
return tasks;
}
}
return tasks;
}
This is what I tried, but it doesn't work, as it throws an InvocationTargetException. I am trying to solve my issue, try to debug it in my brain for like 2 hours now already, but can't sort it out.