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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:50:58  来源:igfitidea点击:

Trying to remove an object with list iterator

javalistiterator

提问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

  1. Do not remove an item from a list directly. Use the remove()method in the iterator.

  2. 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 called hasNextonce. Perhaps you meant:

    while(listIterator.hasNext()){
        Book b = listIterator.next();
        if (b.getBookTitle().equals(bookName)) { 
          // ...
    
  3. Finally, you can replace:

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

    with

    listIterator = books.listIterator();
    
  1. 不要直接从列表中删除项目。使用remove()迭代器中的方法。

  2. 您的代码也存在缺陷,因为它假定还有其他列表项:

    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)) { 
          // ...
    
  3. 最后,您可以替换:

    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 ArrayListwhile iterating.

您不能从ArrayList迭代中删除项目。

you can either :

你可以:

  • Use the remove() method of the ListIterator(at most once for each next())
  • Use another Listimplementation, like CopyOnWriteArrayList, wich is garanteed never to throw an ConcurrentModificationException, 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" 错误。有关该错误的更多参考,请点击链接