Trying to remove an object with list iterator

2020-06-23 09:01发布

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"

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

    }
    }

6条回答
Ridiculous、
2楼-- · 2020-06-23 09:27

instead of 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.

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.

查看更多
3楼-- · 2020-06-23 09:35

You cannot remove an item whilst iterating a list, you should use .remove()

Also remove this:

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

This is not neccesary

查看更多
乱世女痞
4楼-- · 2020-06-23 09:36

instead of books.remove(b)

try to use

   listIterator.remove();
查看更多
狗以群分
5楼-- · 2020-06-23 09:36
{   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

查看更多
爷、活的狠高调
6楼-- · 2020-06-23 09:37
  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 hasNext once. 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();
    
查看更多
三岁会撩人
7楼-- · 2020-06-23 09:47

You can NOT remove an item from an ArrayList while iterating.

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 an ConcurrentModificationException, but this is probably overkill in your case.
查看更多
登录 后发表回答