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

    Default LinkedList and iterators

    I have two problems, problem one is an error messages problem two is a how to question.

    First off i have this error message when trying to run my program: EDIT

    Exception in thread "main" java.util.ConcurrentModificationException

    at java.util.LinkedList$ListItr.checkForComodificatio nLinkedList.java:761)
    at java.util.LinkedList$ListItr.next(LinkedList.java: 696)
    at Registry.deleteAssignment(Registry.java:25)
    at RegistryTester.main(RegistryTester.java:17)
    Java Result: 1

    The message appears here when i try to delete from the linkedlist, the linked list contains two assignments which have already been added and printed to check they are there.

    Java Code:
            System.out.println("Delete assignment with title Java 2");
            System.out.println("***********************************\n");
            
            newAssignment.deleteAssignment("Java");
            System.out.println(newAssignment.toString());
            
        }
    This is the delete method im using, i think the problem is something to do with the iterator from what i have manged to see from searching, as to how to fix it i haven't seen a clear answer, hopefully you can help

    Java Code:
    public class Registry 
    {
        LinkedList<Assignment> assignmentList;
        ListIterator<Assignment> iterator; 
        
        public Registry()
        {
            assignmentList = new LinkedList<Assignment>();
            iterator = assignmentList.listIterator(0);
        }
        
        public void deleteAssignment(String delete)
        {
            
            while(iterator.hasNext())
            {
                if(iterator.next().getATitle().equals(delete))
                    iterator.remove();
            }
                
        }
    Problem two i will elaborate on when this one is sorted, unless it would be easier to start a new topic.
    Last edited by wdh321; 04-25-2012 at 05:54 PM.

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

    Default Re: LinkedList and iterators

    You've missed off most of the exception, chiefly the actual exception name.
    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: LinkedList and iterators

    Sorry net beans had split the lines up and i missed them when copying. Its in full below.

    From what ive seen, im removing an item from the list that the iterator is running through and that is causing the problem. Not sure what to do to fix this though.

    Exception in thread "main" java.util.ConcurrentModificationException

    at java.util.LinkedList$ListItr.checkForComodificatio nLinkedList.java:761)
    at java.util.LinkedList$ListItr.next(LinkedList.java: 696)
    at Registry.deleteAssignment(Registry.java:25)
    at RegistryTester.main(RegistryTester.java:17)
    Java Result: 1
    Last edited by wdh321; 04-25-2012 at 05:59 PM.

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

    Default Re: LinkedList and iterators

    Normally an iterator is created where it is used, and not held onto as an attribute.
    In your case you got hold of the iterator in your constructor and, in between getting it and reading it (ie using next()), you added something to the List the Iterator came from.
    That's not allowed.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: LinkedList and iterators

    Thank you, ive created the iterator in the delete method and that now works how i need it.

Similar Threads

  1. Multi-iterators
    By philwei in forum New To Java
    Replies: 4
    Last Post: 10-04-2011, 11:58 PM
  2. Iterators
    By Archer in forum New To Java
    Replies: 5
    Last Post: 04-09-2011, 01:13 PM
  3. generics and iterators
    By TopNFalvors in forum New To Java
    Replies: 3
    Last Post: 03-29-2011, 06:18 PM
  4. Understanding Iterators
    By Domo230 in forum New To Java
    Replies: 2
    Last Post: 02-12-2011, 01:03 AM
  5. Iterator over Iterators
    By chawlakunal in forum New To Java
    Replies: 2
    Last Post: 05-22-2010, 10:16 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
  •