Thread: LinkedList help
View Single Post
Old 09-29-2008, 01:47 AM   PM User | #2
shadowmaniac
Regular Coder

 
Join Date: Apr 2005
Location: Ohio
Posts: 254
Thanks: 1
Thanked 63 Times in 63 Posts
shadowmaniac is an unknown quantity at this point
Quote:
Originally Posted by unc123w View Post
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.
shadowmaniac is offline   Reply With Quote