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 4272444
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:34:43+00:00 2026-05-21T07:34:43+00:00

I am in a very peculiar state. I have a list something like below

  • 0

I am in a very peculiar state. I have a list something like below :-

List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");

Now when i do multiple type of traversing, like using advanced for, iterator, and normal for loop, below are the sample code snippets :-

1> Advanced Loop :-

try {
    for(String a : list) {
        System.out.println(a);
        list.add("f");
    }
} catch (Exception e) {
    e.printStackTrace();
}

2> Iterator :-

try {
    Iterator<String> itr =  list.iterator();
    while(itr.hasNext()) {
        System.out.println(itr.next());
        list.add("f");
    }
} catch (Exception e) {
    e.printStackTrace();
}

3> Normal Loop :-

for (int i=0;i<list.size();i++) {
    System.out.println(list.get(i));
    list.add("f");
}

Now, the peculiar problem is, that when using advanced for-loop and iterator, i get the following exception : –

java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
    at java.util.ArrayList$Itr.next(Unknown Source)


the reason i know, that while iterating through a list, one cannot modify it parallely.

but when i use the normal for loop, then it works properly, Am i missing something??

Please Help!!!..

  • 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-05-21T07:34:44+00:00Added an answer on May 21, 2026 at 7:34 am

    If you modify a List, it invalidates any Iterator objects created from it. The advanced loop (1) compiles to nearly the same code as the iterator loop (2), meaning that there is an Iterator object created behind the scenes.

    The javadocs for ConcurrentMOdificationException have more details.

    If you want to add while iterating, use

    ListIterator listIter = ...
    while(listIter.hasNext())
    {
        if(shouldAdd(iter.next())) {
            iter.add(....)
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very peculiar problem:(not using web.config) DefaultFacebookApplication dfa=new DefaultFacebookApplication(); dfa.AppId = ...;
A very peculiar bug: I have BarButtonItem which pops out a UIPopoverController like that:
Very simple + silly question: Does clojure provide multi maps? I currently have something
Very new to python and can't understand why this isn't working. I have a
very often I like to have my movieclip code inside the movieclip (on a
Very new to javascript and html-type stuff. I wanted to just make a quick
I have come across what appears to be very peculiar behaviour using entity framework
We are running MS SQL 2005 and we have been experiencing a very peculiar
So very peculiar spring httpinvoker issue going on. I have a soap web service
I have a very peculiar problem. In the VS2010 Server Explorer, I can connect

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.