EDIT: Not sure why but the code seems to be working now without any edits. Could have been a problem with the jGrasp debugger?
===
Ok. So this is my homework assignment that will be assigned 2 weeks from now, but I want a head start. Please do not correct my code, or share the correct code. If you could pin-point the error in what I'm doing, that would be great.
So I have a node
with following constructors:
public node(String name)
public node(String name, node next)
I need to write a method public method(ArrayList<String> names)
in a separate class that will add all elements from names
into the linked list.
Here's what I have right now:
public method(ArrayList<String> names) {
if(names.size() == 0 || names == null) {
throw new IllegalArgumentException();
}
// Handle base case, create first node
first = new node(names.get(0)); // first has been declared above
node current = first;
// Add at the end of the list
for(int i = 1; i < names.size(); i++) {
current.next = new node(names.get(i));
current = current.next;
}
}
I'm not sure why this doesn't work as required. I'm using jGrasp, and using the debugger, I see that at the end, I get a linked list of just 1 value (the last element in the ArrayList). Why?
Please do not recommend using any advanced features, since I'm new to Java and using any further advanced features will simply confuse me.