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
Roney Michael
  • 3,964
  • 5
  • 30
  • 45
  • 1
    How is this supposed to be adding `Student`s to the `List`? – Hunter McMillen Mar 20 '13 at 18:56
  • This might help : http://stackoverflow.com/a/1496221/2087646 – Aboutblank Mar 20 '13 at 18:58
  • That looks like a fine remove method, but not an add method. – Patricia Shanahan Mar 20 '13 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. – Alex Kreutznaer Mar 20 '13 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 –  Mar 20 '13 at 19:50
  • The next method is addStudent(Student aStudent) this takes in an object of Student, and Before it gets added to the Linked List it checks to see if There is a Student There with the same ID, If there is then an error message comes up, If not The studentObject gets added to the linked list –  Mar 20 '13 at 19:54

3 Answers3

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.

Kakalokia
  • 3,191
  • 3
  • 24
  • 42
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()
user2147970
  • 412
  • 2
  • 7
  • 1
    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. – Kakalokia Mar 20 '13 at 19:03
  • Let's look at the remove method of the iterator used by ArrayList public void remove() { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); .... – user2147970 Mar 20 '13 at 19:06
  • We're dealing with a LinkedList here. – Kakalokia Mar 20 '13 at 19:07
  • 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. – Adriaan Koster Mar 20 '13 at 19:12
  • This Doesnt Work Either, Brings Up The Same Error –  Mar 20 '13 at 19:15
  • Sorry This is for the Delete Student Not ADD –  Mar 20 '13 at 19:44
  • Well the making the Iterator Locally has worked, Now how can i do this using One Iterator That Isnt Locally? –  Mar 20 '13 at 19:57
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.

Alex Kreutznaer
  • 1,170
  • 8
  • 18
  • 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. –  Mar 21 '13 at 08:07