I was tring to use that method addGarbage to add an object from the class Garbage to a Garbage linked list.
void addGarbage(List<Garbage> list){
Garbage garbage = new Garbage();
list.add(garbage);
}
addGarbage(GarbageList);
the add method works when using other types, and the GarbageList was declared as a List of the type Garbage.
Here’s its declaration:
List<Garbage> GarbageList = new List<>();
And here is the add method (that works):
void add(T data) {
Node<T> newNode = new Node<T>(data);
if (head == null) {
head = newNode;
} else {
Node end = head;
while (end.next != null) {
end = end.next;
}
end.next = newNode;
}
}
New contributor
TeFurto777 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.