0

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
6
  • 1
    How is this supposed to be adding Students to the List? Commented Mar 20, 2013 at 18:56
  • This might help : stackoverflow.com/a/1496221/2087646 Commented Mar 20, 2013 at 18:58
  • That looks like a fine remove method, but not an add method. Commented Mar 20, 2013 at 19:12
  • Can you please post your original task, if possible? What you can and what you cannot use. It looks like the task is for linked lists behavior and you are limited in what you can use. Commented Mar 20, 2013 at 19:45
  • Ok Here is what im trying to do: Building a student registry system that has the abbillity to add, Delete and view student, so i have 2 methods on my Registry class which are: deleteStudent(int studentID) This takes in an Integer value which will be the Students ID number, Now when i enter an ID in this perameter it will loop through the LinkedList to find a Student object with the Id thats been entered. This then deletes if its found, If its not found then an error message comes up Commented Mar 20, 2013 at 19:50

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

7 Comments

I think you can delete an item while iterating, it's just you can't do it if you are trying to do it during the creation of the list. You need to finish creating your list before iterating over it and modifying it.
Let's look at the remove method of the iterator used by ArrayList public void remove() { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); ....
We're dealing with a LinkedList here.
You can certainly remove items during an iteration; that's the whole point of using an Iterator. You just need to use the Iterator.remove() method, not another deletion method.
This Doesnt Work Either, Brings Up The Same Error
|
0

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.

1 Comment

Cheers for the guidence, I think ive sorted it now, My Iterators were decalred outside the method and i was only Using one for Every Method, If i addedd the Iterators publically to the method they work fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.