【发布时间】:2013-04-18 13:27:12
【问题描述】:
我正在尝试使用列表迭代器从列表中删除一个对象。我已经浏览了网站上的其他解决方案,但都没有缓解错误“线程“main”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
}
}
【问题讨论】:
-
为什么使用 hasPrevious()
-
PSR 我使用 has.previous() 到达列表的开头,以确保在添加或删除后从头开始
-
有些奇怪...您拨打
Book b = listIterator.next();,然后在下一行您不使用b,但再次拨打listIterator.next()。这看起来很奇怪......
标签: java listiterator