Reputation:
Ok , Im trying to add some student object in to a Linked List, But im not allowed to use the .add method of Linked list, So when the user calls the removeStudent Method they enter the sutdents ID number in, Then it checks the List for an Object with that Array
Heres My Code For the Add Method:
public void deleteStudent(int studentID)
{
while (iter.hasNext())
{
Student ob = iter.next();
if (ob.getStudentID() == studentID)
{
iter.remove();
break;
}
}
}
When i run this i get this error:
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.LinkedList$ListItr.checkForComodification(LinkedList.java:953)
at java.util.LinkedList$ListItr.next(LinkedList.java:886)
at student.Registry.deleteStudent(Registry.java:30)
at student.Registry.main(Registry.java:51)
Java Result: 1
Upvotes: 0
Views: 4441
Reputation: 1170
Your original task is not clear, but it looks like you are limited by LinkedList API.
And nothing else.
With linked lists delete (and modification in general) of an element is not an easy task - you may have to safely iterate over the whole list. (The good news about linked lists is that insert is easy).
This will work (there are other ways to do it, too):
public void deleteStudent(int studentID){
...
LinkedList<Student> modifiedStudentList = new LinkedList<>();
while (iter.hasNext()){
Student ob = iterator.next();
if (ob.getStudentID() != studentID){
modifiedStudentList.addLast(ob)
}
}
studentList = modifiedStudentList;
}
As the result your list will contain all students it had before, except for the student with studentID, if it doesn't exist in the list, nothing will be deleted.
You will have to iterate over all elements in the collection, but this is the price for the API limitation your teacher set.
Upvotes: 0
Reputation: 412
EDIT : Try to use a local iterator:
public void deleteStudent(int studentID){
Iterator<Student> iterator=listStudent.iterator();
while (iter.hasNext()){
Student ob = iterator.next();
if (ob.getStudentID() == studentID){
iterator.remove(student);
break;
}
}
}
In this way, there is no concurrent modification between the list and your local iterator. But this will modify the list and you will probably have problems if you keep trying to use your previous iterator after calling this method.
EDIT : The AbstractList maintains a "modCount"(modification count) attribute which counts the number of add, remove etc that you made on the list.
When you get an iterator on a List, the iterator remembers this modCount to ensure that you do not edit the list with methods outside of the iterator.
Example:
List myList=new ArrayList();
//modCount for the list is 0
myList.add("test");
//modCount for the list is 1
Iterator iterator=myList.iterator();
//modCount for the list is 1
//expected modCount for the iterator is initialized to 1
myList.add("test 2");
//modCount for the list is 2
//expected modCount for the iterator is initialized to 1
iterator.remove("test");
//modCount != expectedModCount => throw new ConcurrentModificationException()
Upvotes: 0
Reputation: 3191
The ConcurrentModificationException
basically means that you have modified the list between creating the list iterator and using it. What you need to do is create and use the iterator AFTER you have added everything to the list, or modified it by any other means.
Upvotes: 3