Issue
We were given the assignment to write several methods to help add functionality to a TodoList. One of the methods wanted was one that goes through the TodoList and returns the ones which are still incomplete.
This is what I have written (with other methods omitted):
public class TodoList {
private ArrayList<TodoItem> todoItems;
public TodoList() {
todoItems = new ArrayList<TodoItem>();
}
public List<TodoItem> filterIncompletes () {
List<TodoItem> incs = todoItems; *****
for (int i = 0; i <todoItems.size(); i++) {
if (todoItems.get(i).isCompleted() == false) {
incs.add(todoItems.get(i));
}
}
return incs;
}
I have put asterisks on the line where I believe to be going wrong, although I am not positive. I understand that the method wants a List<TodoItem> returned, I'm just not sure if I have that correctly.
Solution
Change
List<TodoItem> incs = todoItems;
to
List<TodoItem> incs = new ArrayList<TodoItem>();
Answered By - Maria Ines Parnisari
Answer Checked By - Robin (JavaFixing Admin)