3

リスト反復子を使用してリストからオブジェクトを削除しようとしています。私はウェブサイトで他の解決策を試しましたが、「スレッド「メイン」java.util.ConcurrentModificationExceptionの例外」というエラーを軽減したものはありません

これが実行されていない私のコードです:

void PatronReturn(String bookName) {
//       get to beginning
    while(listIterator.hasPrevious()) {
        listIterator.previous();
    }
    while(listIterator.hasNext()){
        Book b = listIterator.next();
    if (listIterator.next().getBookTitle().equals(bookName)) { 
        //listIterator.next();
        //listIterator.remove();
        books.remove(b);
        //listIterator.next(); //moves to next so iterator can remove previous ?
        //books.remove(listIterator.next());; // TODO see if this is correct

    }
    }
4

6 に答える 6

0

リストの反復中にアイテムを削除することはできません。.remove() を使用する必要があります。

これも削除します:

while(listIterator.hasPrevious()) {
    listIterator.previous();
}

これは必要ありません

于 2013-04-18T13:36:47.943 に答える