Results 1 to 14 of 14
Thread: Linked List removeAll Help
- 09-24-2008, 11:40 PM #1
Linked List removeAll Help
I'm trying to get my removeAll method to go through my list of numbers and remove all instances of the one I call it to remove, in this case number 6 in list3. Help!!!!! :eek:
Java Code:import java.util.Iterator; import java.util.LinkedList; class ExtendedLinkedList<T> extends MyLinkedList<T> { private Object current; public void print() { java.util.Iterator<T> it = this.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } public ExtendedLinkedList<T> copy2() { java.util.Iterator<T> it = this.iterator(); while(it.hasNext()) { MyLinkedList<T> l = new MyLinkedList<T>(); l.add(it.next()); System.out.print(l); System.out.print(" "); System.out.print(l); System.out.print(" "); } return null; } public ExtendedLinkedList<T> clip(int i, int j) { MyLinkedList<T> l = new MyLinkedList<T>(); l = new MyLinkedList<T>(); return null; } public void removeAll(T x) { ExtendedLinkedList<Integer> list = new ExtendedLinkedList(); int index = 0; while(index < list.size()) { Integer eLL = list.get(index); if(eLL.equals(x)) { list.removeAll((Integer)x); } else { index++; } } } public int sublist(ExtendedLinkedList<T> y) { } class Prog { public static void main (String [] args) { ExtendedLinkedList<Integer> list1 = new ExtendedLinkedList<Integer>(); list1.add(5); list1.add(10); list1.add(15); list1.add(20); list1.print(); ExtendedLinkedList<Integer> list2 = new ExtendedLinkedList<Integer>(); list2.add(3); list2.add(4); list2.add(6); list2.add(8); list2.add(9); list2.copy2(); ExtendedLinkedList<Integer> list3 = new ExtendedLinkedList<Integer>(); list3.add(6); list3.add(4); list3.add(6); list3.add(8); list3.add(9); list3.removeAll(6); list3.print(); } }
- 09-25-2008, 12:16 AM #2
Try debugging your code by adding some println statements to it to see the values of the variables.
Some of the confusion is probaby due to "boxing". Its not clear when an Integer is converted to an int or visa versa.
list3.removeAll(6);
Here is the int converted to an Integer?
What does the Integer.equals() do?
Perhaps the compareTo method would be better.
- 09-25-2008, 01:41 AM #3
watch your removeAll method,there you call the same method from the method,and that's your problem,then it is a recursion.
Another stuff more elegant to reach the specific item (in your case Integer) in the list with Iterator.
- 09-25-2008, 02:56 AM #4
Yeah. Missed that recursive call. He's lucky that the list he's searching in removeAll will always be empty. He should see that if he puts println statements in the code to show logic flow and variable values.
- 09-25-2008, 03:20 AM #5
Ok so I tried changing it up a bit by using an iterator again instead. The method my parent class will remove a node of anytype, so I called it but I'm unsure on how to get my new list to print with the numbers removed.
Java Code:public void removeAll(T x) { java.util.Iterator<T> it = this.iterator(); while(it.hasNext()) { LinkedList<T> list = new LinkedList(); list.remove(x); } }Last edited by unc123w; 09-25-2008 at 03:40 AM.
- 09-25-2008, 12:42 PM #6
Create a new LinkedList which will contain the removed items.
- 09-25-2008, 04:29 PM #7
I'm confused on how this code is to work.LinkedList<T> list = new LinkedList();
If you create a new list here, that list will be empty.
There will be nothing in it to remove.
How does the x variable relate to the iterator?
Where is the list that you want to remove all the elements from?
- 09-26-2008, 03:02 PM #8
I don't know either.:(
- 09-26-2008, 03:41 PM #9
That makes it hard.
If there is no reason for
ExtendedLinkedList<Integer> list = new ExtendedLinkedList();
remove it from the code.
If there is a reason, please explain.
- 09-28-2008, 08:23 PM #10
The list that I want to remove all the elements from is in the main method.. sorry for posting the same code again.. but is this the direction you would point me in?
Java Code:import java.util.Iterator; import java.util.LinkedList; class ExtendedLinkedList<T> extends MyLinkedList<T> { private Object current; public void print() { java.util.Iterator<T> it = this.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } public ExtendedLinkedList<T> copy2() { java.util.Iterator<T> it = this.iterator(); while(it.hasNext()) { MyLinkedList<T> l = new MyLinkedList<T>(); l.add(it.next()); System.out.print(l); System.out.print(" "); System.out.print(l); System.out.print(" "); } return null; } public ExtendedLinkedList<T> clip(int i, int j) { MyLinkedList<T> l = new MyLinkedList<T>(); l = new MyLinkedList<T>(); return null; } public void removeAll(T x) { java.util.Iterator<T> it = this.iterator(); while(it.hasNext()) { T object = it.next(); it.remove(x); System.out.println(it.next()); } } public int sublist(ExtendedLinkedList<T> y) { LinkedList<T> list= new LinkedList(); ExtendedLinkedList<T> subList = null; System.out.println("The new subList: " + list.size() + ": " + subList); System.out.println(); return -1; } } class Prog { public static void main (String [] args) { //ExtendedLinkedList<Integer> list1 = new ExtendedLinkedList<Integer>(); //list1.add(5); //list1.add(10); //list1.add(15); //list1.add(20); //list1.print(); // //ExtendedLinkedList<Integer> list2 = new ExtendedLinkedList<Integer>(); //list2.add(3); //list2.add(4); //list2.add(6); //list2.add(8); //list2.add(9); //list2.copy2(); ExtendedLinkedList<Integer> list3 = new ExtendedLinkedList<Integer>(); list3.add(3); list3.add(4); list3.add(6); list3.add(3); list3.add(9); list3.removeAll(3); list3.print(); } }
- 09-28-2008, 08:32 PM #11
By executing list3.removeAll(3); do you mean to remove only the Integer on the third place?
In your removeAll(T x) method it removes all the elements after the place x.
- 09-28-2008, 08:45 PM #12
I want to remove all the 3's in the list.
- 09-29-2008, 02:28 PM #13
Perhaps you could change the name of the method from removeAll to something like removeAllEqualTo. removeAll usually means ALL of the elements, not a selected subset.
To debug your code, add some println()s in the method to show values and flow of execution. Seeing the output should help you understand what is happening.
You call it.next() two times in your method. Each time will get a value. But you only process one of them. The other one is only displayed with println.Last edited by Norm; 09-29-2008 at 02:31 PM.
- 09-30-2008, 02:41 PM #14
Why do you keep calling new all the time? This will not propogate to original dataset, if you want new lists without the 3 there will have to be a named variable such as
which is correct if you are just showing the results on the console - which is what you are doing - but will have to be placed somewhere if you want to go beyond having the numbers scroll off the screen.Java Code:ExtendedLinkedList<Integer> list2 = new ExtendedLinkedList<Integer>();
Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Similar Threads
-
Circular Double Linked List
By theonly in forum Advanced JavaReplies: 3Last Post: 12-06-2009, 05:10 PM -
Linked List help
By neobie in forum New To JavaReplies: 8Last Post: 12-22-2007, 03:15 AM -
going from vectors to linked list?
By cbrown08 in forum New To JavaReplies: 3Last Post: 12-01-2007, 12:55 AM -
Linked List
By rnavarro9 in forum New To JavaReplies: 0Last Post: 11-29-2007, 03:42 AM -
Help with linked list
By trill in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:29 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks