Results 1 to 5 of 5
Thread: LinkedList and iterators
- 04-25-2012, 04:24 PM #1
Senior Member
- Join Date
- Aug 2011
- Posts
- 116
- Rep Power
- 0
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.
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 helpJava Code:System.out.println("Delete assignment with title Java 2"); System.out.println("***********************************\n"); newAssignment.deleteAssignment("Java"); System.out.println(newAssignment.toString()); }
Problem two i will elaborate on when this one is sorted, unless it would be easier to start a new topic.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(); } }Last edited by wdh321; 04-25-2012 at 04:54 PM.
- 04-25-2012, 04:45 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 04-25-2012, 04:53 PM #3
Senior Member
- Join Date
- Aug 2011
- Posts
- 116
- Rep Power
- 0
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: 1Last edited by wdh321; 04-25-2012 at 04:59 PM.
- 04-25-2012, 05:00 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 04-25-2012, 05:13 PM #5
Senior Member
- Join Date
- Aug 2011
- Posts
- 116
- Rep Power
- 0
Similar Threads
-
Multi-iterators
By philwei in forum New To JavaReplies: 4Last Post: 10-04-2011, 10:58 PM -
Iterators
By Archer in forum New To JavaReplies: 5Last Post: 04-09-2011, 12:13 PM -
generics and iterators
By TopNFalvors in forum New To JavaReplies: 3Last Post: 03-29-2011, 05:18 PM -
Understanding Iterators
By Domo230 in forum New To JavaReplies: 2Last Post: 02-12-2011, 12:03 AM -
Iterator over Iterators
By chawlakunal in forum New To JavaReplies: 2Last Post: 05-22-2010, 09:16 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks