Results 1 to 14 of 14
  1. #1
    unc123w's Avatar
    unc123w is offline Member
    Join Date
    Sep 2008
    Posts
    18
    Rep Power
    0

    Default 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();
    
    
    }
    
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default

    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.

  3. #3
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    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.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default

    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.

  5. #5
    unc123w's Avatar
    unc123w is offline Member
    Join Date
    Sep 2008
    Posts
    18
    Rep Power
    0

    Default

    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 04:40 AM.

  6. #6
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Create a new LinkedList which will contain the removed items.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default

    LinkedList<T> list = new LinkedList();
    I'm confused on how this code is to work.
    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?

  8. #8
    unc123w's Avatar
    unc123w is offline Member
    Join Date
    Sep 2008
    Posts
    18
    Rep Power
    0

    Default

    I don't know either.:(

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default

    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.

  10. #10
    unc123w's Avatar
    unc123w is offline Member
    Join Date
    Sep 2008
    Posts
    18
    Rep Power
    0

    Default

    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();
    
    
    
    }
    
    }

  11. #11
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    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.

  12. #12
    unc123w's Avatar
    unc123w is offline Member
    Join Date
    Sep 2008
    Posts
    18
    Rep Power
    0

    Default

    I want to remove all the 3's in the list.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default

    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 03:31 PM.

  14. #14
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    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
    Java Code:
    ExtendedLinkedList<Integer> list2 = new ExtendedLinkedList<Integer>();
    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.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. Circular Double Linked List
    By theonly in forum Advanced Java
    Replies: 3
    Last Post: 12-06-2009, 06:10 PM
  2. Linked List help
    By neobie in forum New To Java
    Replies: 8
    Last Post: 12-22-2007, 04:15 AM
  3. going from vectors to linked list?
    By cbrown08 in forum New To Java
    Replies: 3
    Last Post: 12-01-2007, 01:55 AM
  4. Linked List
    By rnavarro9 in forum New To Java
    Replies: 0
    Last Post: 11-29-2007, 04:42 AM
  5. Help with linked list
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 08:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •