2013-03-20 4 views
0

링크 된 목록에 일부 학생 개체를 추가하려하지만 링크 된 목록의 .add 메서드를 사용할 수 없으므로 사용자가 removeStudent 메서드를 호출하면Iterator를 사용하여 연결된 목록에 개체 추가

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 
+1

'List'에'Student's를 어떻게 추가해야합니까? –

+0

도움이 될 수 있습니다 : http://stackoverflow.com/a/1496221/2087646 – Aboutblank

+0

그건 좋은 제거 방법처럼 보이지만 add 방법은 아닙니다. –

답변

0
:이 프로그램을 실행할 때

public void deleteStudent(int studentID) 
{ 
    while (iter.hasNext()) 
    { 
     Student ob = iter.next(); 
     if (ob.getStudentID() == studentID) 
     { 
     iter.remove(); 
     break; 
     } 
    } 
    } 

내가이 오류 : 그런 다음이 추가 방법이 배열

Heres는 내 코드와 객체의 목록을 확인에 ID 번호를 sutdents

편집 : 로컬 반복자를 사용해보십시오 :이 방법으로

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; 
    } 
    } 
} 

는 목록과 해당 지역의 반복자 사이의 동시 변경이 없습니다. 그러나이 방법은 목록을 수정하고이 메서드를 호출 한 후에 이전 반복기를 계속 사용하려고하면 문제가 발생할 수 있습니다.

EDIT : AbstractList는 추가, 제거 등의 수를 계산하는 "modCount"(수정 횟수) 속성을 유지합니다.

목록에 반복기를 가져 오는 경우 반복기는이 modCount를 기억하여 반복기 외부의 메서드로 목록을 편집하지 않도록합니다.

예 :

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

반복하는 동안 항목을 삭제할 수 있다고 생각합니다. 목록을 만드는 동안 그것을하려고합니다. 목록을 반복하고 수정하기 전에 목록 작성을 완료해야합니다. –

+0

ArrayList에 의해 사용되는 반복자의 remove 메소드를 보자. public void remove() { if (lastRet <0) 새로운 IllegalStateException()을 던진다. checkForComodification(); .... – user2147970

+0

여기에서 LinkedList를 다루고 있습니다. –

3

ConcurrentModificationException은 기본적으로 당신이리스트 반복자를 생성하고 사용 사이의 목록을 수정 한 것을 의미한다. 당신이해야 할 일은 목록에 모든 것을 추가하거나 다른 방법으로 수정 한 후에 반복자를 생성하고 사용하는 것입니다.

0

원래 작업이 명확하지 않지만 LinkedList API로 인해 제한적으로 보입니다.

그 밖의 것은 없습니다.

링크 된 목록을 사용하면 요소의 삭제 (및 일반적으로 수정)가 쉬운 작업이 아니므로 전체 목록을 안전하게 반복해야 할 수 있습니다. 링크 된 목록에 대한 좋은 소식은 삽입이 쉽다는 것입니다.

이 (가 그것을 할 수있는 다른 방법도 있습니다) 작동합니다 : 결과적으로

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; 
} 

은 '아무튼 경우, 그것은 studentID와 학생을 제외하고, 이전에 가진 모든 학생을 포함 목록 목록에 없으면 아무 것도 삭제되지 않습니다.

컬렉션의 모든 요소를 ​​반복해야하지만 이는 교사가 설정 한 API 제한의 가격입니다.

+0

건배에 대한 건배, 나는 지금 그것을 ive 생각, 내 반복기는 방법 밖에 서 decaled했다 그리고 나는 단지 모든 메서드에 대한 하나를 사용하는 경우, 나는 그들이 잘 작동하는 방법에 공개 iterators을 추가 했어. –

관련 문제