:이 프로그램을 실행할 때
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()
'List'에'Student's를 어떻게 추가해야합니까? –
도움이 될 수 있습니다 : http://stackoverflow.com/a/1496221/2087646 – Aboutblank
그건 좋은 제거 방법처럼 보이지만 add 방법은 아닙니다. –