Quote:
Originally Posted by unc123w
Code:
public void removeAll(T x) {
java.util.Iterator<T> it = this.iterator();
while(it.hasNext()) {
LinkedList l = new LinkedList(); // list is empty and not initialized
T object = x;
l.remove(x);
System.out.println(it.next());
System.out.println(l);
}
}
|
The reason you're getting an empty list as output is because every time you go to the while loop, you create an empty list "l". You want to create the actual list outside the loop and then initialize it to the values of list3. Basically something like this:
Code:
public void removeAll(T x) {
java.util.Iterator<T> it = this.iterator();
LinkedList l = new LinkedList();
l.addAll(<<<<reference to list3>>>>) //initialize l
while(it.hasNext()) {
T object = x;
l.remove(x);
System.out.println(it.next());
System.out.println(l);
}
}
Don't hold me up on the validity of the above code though. Still, my "observations" should be good.