Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7731209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:25:23+00:00 2026-06-01T06:25:23+00:00

I got an issue with deleting an object from ArrayList when working on the

  • 0

I got an issue with deleting an object from ArrayList when working on the assignment
If I use the “normal” for loop, it works as following

public void returnBook(String isbn){        
    for (int i = 0; i < booksBorrowed.size(); i++){            
        if (booksBorrowed.get(i).getISBN() == isbn){
            booksBorrowed.get(i).returnBook();
            booksBorrowed.remove(i);                
        }
    }
}

However, when I’m trying to simplify the code with enhanced for-loop, that doesn’t work and showing java.util.ConcurrentModificationException error:

public void returnBook(String isbn){        
        for (Book book: booksBorrowed){            
            if (book.getISBN() == isbn){
                book.returnBook();
                booksBorrowed.remove(book);                
            }
        }
}

Hope you guys could lighten me up..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T06:25:25+00:00Added an answer on June 1, 2026 at 6:25 am

    Your alternatives to avoid a ConcurrentModificationException are:

    List<Book> books = new ArrayList<Book>();
    books.add(new Book(new ISBN("0-201-63361-2")));
    books.add(new Book(new ISBN("0-201-63361-3")));
    books.add(new Book(new ISBN("0-201-63361-4")));
    

    Collect all the records that you want to delete on enhanced for loop, and after you finish iterating, you remove all found records.

    ISBN isbn = new ISBN("0-201-63361-2");
    List<Book> found = new ArrayList<Book>();
    for(Book book : books){
        if(book.getIsbn().equals(isbn)){
            found.add(book);
        }
    }
    books.removeAll(found);
    

    Or you may use a ListIterator which has support for a remove method during the iteration itself.

    ListIterator<Book> iter = books.listIterator();
    while(iter.hasNext()){
        if(iter.next().getIsbn().equals(isbn)){
            iter.remove();
        }
    }
    

    Or you may use a third-party library like LambdaJ and it makes all the work for you behind the scenes>

    List<Book> filtered = select(books, 
                    having(on(Book.class).getIsbn(), 
                            is(new ISBN("0-201-63361-2"))));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Got an issue where MyObj.classnameis(TMyClass.classname) is true and TMyClass(MyObj) works but (MyObj as TMyclass).doSomething
I got some issue working with UINavigationalControllers for iPad. Issue : UINavigationController1 UINavigationController2 Placed
I've placed panel from this example http://docs.sencha.com/ext-js/4-0/#!/example/dd/dnd_grid_to_grid.html to Ext.window.Window and got an issue: proxy
I got an issue from my client regarding the SSL setup for his website.
I've got an issue with an object not being deallocated in objective-c. I'm pretty
i got an issue on firefox, while all IE, Safari and chrome are working.
I got following issue: That's work: #include <QtCore/QCoreApplication> #include <QColor> int main(int argc, char
I've got another issue. My method: public StudentProfile GetFullStudentProfile(int userID) { SqlConnection conn =
I've got an issue similar to this question here: Javascript and AJAX, only works
I've got an issue where i'm trying to call an object in spring.net through

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.