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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:55:42+00:00 2026-06-05T19:55:42+00:00

if have the following problem: I have a List which i am going through

  • 0

if have the following problem:
I have a List which i am going through using the enhanced for loop. Every time i want to remove sth, out of the list, i get a ConcurrentModificationException. I already found out why this exception is thrown, but i don`t know how i can modify my code, so that its working. This is my code:

for(Subject s : SerData.schedule)
    {
        //Checking of the class is already existing
        for(Classes c : s.classes)
        {
            if(c.day == day &c.which_class == which_class)
            {
                int index = getclassesindex(s.classes, new Classes(day, which_class));
                synchronized (s) {
                    s.classes.remove(index);

                }
            }
        }
            //More code....
    }

I also tried out this implementation.

for(Subject s : SerData.schedule)
    {
        //Checking of the class is already existing
        Iterator<Classes> x = s.classes.iterator();
        while(x.hasNext())
        {
            Classes c = x.next();
            if(c.day == day &c.which_class == which_class)
            {
                int index = getclassesindex(s.classes, new Classes(day, which_class));
                synchronized (s) {
                    s.classes.remove(index);
                }
            }
        }
        //More code....
    }

not working either…

Is there a common used, standard solution? (Hopefully sth. that is not obvious 😀 )

  • 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-05T19:55:43+00:00Added an answer on June 5, 2026 at 7:55 pm

    The main reason this issue occurs is because of the semantic meaning of your for-each loop.

    When you use for-each loops, the data structure that is being traversed cannot be modified.

    Essentially anything of this form will throw this exception:

    for( Object o : objCollection )
    {
        // ...
        if ( satisfiesSomeProperty ( o ) )
           objList.remove(o);    // This is an error!!
        // ...
    }
    

    As a side note, you can’t add or replace elements in the collection either.

    There are a few ways to perform this operation.

    One way is to use an iterator and call the remove() method when the object is to be removed.

    Iterator <Object> objItr = objCollection.iterator();
    
    while(objItr.hasNext())
    {
        Object o = objItr.next();
        // ...
        if ( satifiesSomeProperty ( o ) )
            objItr.remove();    // This is okay
        // ...
    }
    

    This option has the property that removal of the object is done in time proportional to the iterator’s remove method.

    The next option is to store the objects you want to remove, and then remove them after traversing the list. This may be useful in situations where removal during iteration may produce inconsistent results.

    Collection <Object> objsToRemove = // ...
    for( Object o : objCollection )
    {
        // ...
        if ( satisfiesSomeProperty ( o ) )
           objsToRemove.add (o);
        // ...
    }
    objCollection.removeAll ( objsToRemove );
    

    These two methods work for general Collection types, but for lists, you could use a standard for loop and walk the list from the end of the list to the front, removing what you please.

    for (int i = objList.size() - 1; i >= 0; i--)
    {
        Object o = objList.get(i);
        // ...
        if ( satisfiesSomeProperty(o) )
           objList.remove(i);
        // ...
    }
    

    Walking in the normal direction and removing could also be done, but you would have to take care of how incrementation occurs; specifically, you don’t want to increment i when you remove, since the next element is shifted down to the same index.

    for (int i = 0; i < objList.size(); i++)
    {
        Object o = objList.get(i);
        // ...
        if ( satisfiesSomeProperty(o) )
        {
           objList.remove(i);
           i--;
        }
    
        //caveat: only works if you don't use `i` later here
        // ...
    }
    

    Hope this provides a good overview of the concepts and helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with following script. It generates a list of places which
I have the following problem: list.c struct nmlist_element_s { void *data; struct nmlist_element_s *next;
I have following problem. In my view model I defined some list properties as
I have a <select> list, which has been populated with several options, but want
I have the following problem, on which I can't seem to find an answer...
I have the following code which recursively operates on each element within a List
I am stuck with this following problem. Say, I have a request which has
I have the following JQuery which allows a user to select/de-select a list of
I have long file with the following list: /drivers/isdn/hardware/eicon/message.c//add_b1() /drivers/media/video/saa7134/saa7134-dvb.c//dvb_init() /sound/pci/ac97/ac97_codec.c//snd_ac97_mixer_build() /drivers/s390/char/tape_34xx.c//tape_34xx_unit_check() (PROBLEM)/drivers/video/sis/init301.c//SiS_GetCRT2Data301() /drivers/scsi/sg.c//sg_ioctl()
I have following problem: I have built a tabbar application with 4 tabs. I

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.