Results 1 to 16 of 16
Like Tree1Likes
  • 1 Post By Norm

Thread: Remove matching LinkedList item

  1. #1
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Remove matching LinkedList item

    Im creating a program that stores assignments. The assignments are stored in a linked list.
    I want to be able to delete an assignment based on its title.
    I know how to traverse a linked list but i dont know how find the title within the assignments on the linked list and then delete that specific assignment.

    The assignments have a number of different variables such as module title, name, date due etc. The only unique variable will be the assignment title.

    Any help on how to do this would be great,
    Thanks

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Remove matching LinkedList item

    Since you know how to traverse the list, then simply compare the title of each assignment with the title you are searching for.
    When you have a match then delete that entry.

    If this is a LinkedList then you'd simply use the Iterator.delete() method (or I would anyway).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: Remove matching LinkedList item

    Im a bit confused about finding the title within the assignments.
    I have iterator.hasNext() to traverse the assignments, i also have methods in the assignment class to return the assignment title but im not sure how to compare these. I assume itll be something along the lines of getAssignmentTitle.equals(title) ??

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

    Default Re: Remove matching LinkedList item

    That looks close. You would need a reference to an object to call one of its methods. The Iterator will return a reference to an object in the list. Use that reference to call the method:

    refToObject.theMethod();
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: Remove matching LinkedList item

    Ive got:

    Java Code:
    ListIterator<Assignment> iterator = assignmentList.listIterator();
    
    public void deleteAssignment(String title)
        {
            while(iterator.hasNext())
            {
                if(Assignment.getATitle().equals(title));
            }
                
        }
    Only problem with this is im getting an error "non-static method getATitile() cannot be referenced from a static context". I assume it wants me to create an Assignment first, that wont be right in this situation though, as the idea is the assignments are already there and this method simply looks through them. If im making sense

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

    Default Re: Remove matching LinkedList item

    error "non-static method getATitile() cannot be referenced from a static context"
    Your syntax for calling the getATitle() method is for a static method. In place of the name of the class:Assignment you need a reference to an instance of the class that contains the data you want access to.
    The Iterator class has a method you need to call to get the reference you need. When you get the reference then use it to call the method.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: Remove matching LinkedList item

    Thanks, this is what i have now. NetBeans seems happy with it.

    Java Code:
    public void deleteAssignment(String title)
        {
            while(iterator.hasNext())
            {
                if(iterator.next().getATitle().equals(title))
                    iterator.remove();
            }
                
        }

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

    Default Re: Remove matching LinkedList item

    Does it work as you want?
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: Remove matching LinkedList item

    Im creating a tester class to test the methods now.
    There seems to be an issue with creating the ListIterator in my Registry class, i know its obvious as ive had a similar error message before i just cant seem to see it.
    Error Message:
    Exception in thread "main" java.lang.NullPointerException
    at Registry.<init>(Registry.java:7)
    at RegistryTester.main(RegistryTester.java:5)
    Java Result: 1

    Java Code:
    public class Registry 
    {
        LinkedList<Assignment> assignmentList;
        ListIterator<Assignment> iterator = assignmentList.listIterator();
        
        public Registry()
        {
            assignmentList = new LinkedList<Assignment>();
        }

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

    Default Re: Remove matching LinkedList item

    Exception in thread "main" java.lang.NullPointerException
    at Registry.<init>(Registry.java:7)
    What variable on line 7 has a null value? Is that line the same as line 4 in the posted code?
    The code uses assignmentList before it has a value.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: Remove matching LinkedList item

    The LinkedList assignmentList is empty.
    line 5 in RegistryTester is "Registry assignment = new Registry();"
    Am i being blind because ive be staring at the screen most the day?

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

    Default Re: Remove matching LinkedList item

    Is your code working now?

    The LinkedList assignmentList is empty.
    There is a difference between an empty list and a list that does not exist.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: Remove matching LinkedList item

    Ive changed it to this;

    Java Code:
        LinkedList<Assignment> assignmentList;
        ListIterator<Assignment> iterator; 
        
        public Registry()
        {
            assignmentList = new LinkedList<Assignment>();
            iterator = new ListIterator<Assignment>();
        }
    That way im not using assingmentList before it has a value, however i get compile errors because i cant instantiate ListIterator because its an interface. A search didnt show much but it suggest using an arraylist to instaniate it, have you got any suggestions?

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

    Default Re: Remove matching LinkedList item

    cant instantiate ListIterator because its an interface.
    The LinkedList class has a method you should call to get the Iterator.
    wdh321 likes this.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: Remove matching LinkedList item

    Thanks! Found it, that will teach me to look more closely at the documentation

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

    Default Re: Remove matching LinkedList item

    That is a good habit and useful skill to have.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. getting first item in a linkedList
    By TopNFalvors in forum New To Java
    Replies: 4
    Last Post: 04-01-2011, 04:26 PM
  2. NullPointer adding an item to a LinkedList
    By sehudson in forum New To Java
    Replies: 7
    Last Post: 03-10-2011, 04:39 AM
  3. Button to remove item from list
    By dacool25 in forum Java Applets
    Replies: 6
    Last Post: 10-14-2009, 05:30 AM
  4. Remove an item from listbox
    By Dieter in forum Advanced Java
    Replies: 9
    Last Post: 09-21-2009, 11:40 PM

Posting Permissions

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