java 尝试使用列表迭代器删除对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16084279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to remove an object with list iterator
提问by shrimpah
I'm trying to remove an object from a list using the list iterator. I've gone through the other solutions on the website and none have alleviated the error "Exception in thread "main" java.util.ConcurrentModificationException"
我正在尝试使用列表迭代器从列表中删除一个对象。我已经浏览了网站上的其他解决方案,但都没有缓解错误“线程“main”中的异常java.util.ConcurrentModificationException”
here is my code that is not executing :
这是我没有执行的代码:
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
}
}
回答by Duncan Jones
Do not remove an item from a list directly. Use the
remove()
method in the iterator.Your code is also flawed in that it assumes there are additional list items:
while(listIterator.hasNext()){ Book b = listIterator.next(); if (listIterator.next().getBookTitle().equals(bookName)) { // eek
Here you call
next()
twice, yet you've only calledhasNext
once. Perhaps you meant:while(listIterator.hasNext()){ Book b = listIterator.next(); if (b.getBookTitle().equals(bookName)) { // ...
Finally, you can replace:
while(listIterator.hasPrevious()) { listIterator.previous(); }
with
listIterator = books.listIterator();
不要直接从列表中删除项目。使用
remove()
迭代器中的方法。您的代码也存在缺陷,因为它假定还有其他列表项:
while(listIterator.hasNext()){ Book b = listIterator.next(); if (listIterator.next().getBookTitle().equals(bookName)) { // eek
在这里你打了
next()
两次电话,但你只打了hasNext
一次电话。也许你的意思是:while(listIterator.hasNext()){ Book b = listIterator.next(); if (b.getBookTitle().equals(bookName)) { // ...
最后,您可以替换:
while(listIterator.hasPrevious()) { listIterator.previous(); }
和
listIterator = books.listIterator();
回答by duffy356
instead of books.remove(b)
而不是books.remove(b)
use
利用
listIterator.remove();
the reason is, that the iterator gives you the next() book, if you only remove the book from books, the iterator has the "removed" book still as next book.
原因是,迭代器给你 next() 书,如果你只从书中删除这本书,迭代器会将“删除”的书作为下一本书。
it did not work in your code, because you called .next() twice, once for the book b and a second time when comparing the book title, with the next book.
它在您的代码中不起作用,因为您调用了 .next() 两次,一次用于书籍 b,第二次用于将书名与下一本书进行比较。
回答by PSR
instead of books.remove(b)
代替 books.remove(b)
try to use
尝试使用
listIterator.remove();
回答by bwt
You can NOT remove an item from an ArrayList
while iterating.
您不能从ArrayList
迭代中删除项目。
you can either :
你可以:
- Use the remove() method of the
ListIterator
(at most once for each next()) - Use another
List
implementation, like CopyOnWriteArrayList, wich is garanteed never to throw anConcurrentModificationException
, but this is probably overkill in your case.
- 使用(
ListIterator
每个next()最多一次)的remove()方法 - 使用另一种
List
实现,例如CopyOnWriteArrayList,保证永远不会抛出ConcurrentModificationException
,但这在您的情况下可能是矫枉过正。
回答by JeroenVP
You cannot remove an item whilst iterating a list, you should use .remove()
您不能在迭代列表时删除项目,您应该使用 .remove()
Also remove this:
也删除这个:
while(listIterator.hasPrevious()) {
listIterator.previous();
}
This is not neccesary
这不是必需的
回答by Madhusudan Joshi
{ Book toBeReaplced = null; Book tempBook = null;
void PatronReturn(String bookName)
{// get to beginning
while(listIterator.hasPrevious()) { listIterator.previous();
}
while(listIterator.hasNext()){
Book tempBook = listIterator.next();
if (b.getBookTitle().equals(bookName)) {
toBeReaplced = tempBook;
}
listIterator.remove(bookToBeReplaced);
}
You can try the above code. I think it will able to solve your problem, that you have facing that java.util.ConcurrentModificationException" error. for more reference on the error please follow the link
你可以试试上面的代码。我认为它能够解决您的问题,即您遇到了 java.util.ConcurrentModificationException" 错误。有关该错误的更多参考,请点击链接