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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:07:59+00:00 2026-05-27T00:07:59+00:00

Following is the errorneous code in Java. package wrapper; import java.util.ArrayList; import java.util.Iterator; import

  • 0

Following is the errorneous code in Java.

package wrapper;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

final public class Main 
{
    public static void main(String... args) 
    {
        List<String>list=new ArrayList<String>();

        list.add("A");
        list.add("B");
        list.add("C");

        Iterator<String>it=list.iterator();

        while(it.hasNext())
        {
            String item=it.next();

            if(item.equals("B"))
            {
                list.remove(item);
            }
            System.out.println(item);
        }


        ListIterator<String>listIterator=list.listIterator();

        listIterator.set("D");       //Throws java.lang.IllegalStateException
        listIterator.add("D");

        System.out.print("\n\n");
        for(Object o:list)
        {
            System.out.println(o);
            if(o.equals("C"))
            {
                listIterator.remove();    //Throws java.lang.IllegalStateException
            }
        }
    }
}

While handling entities in EJBs or otherwise, the use of List, Collection, Set etc is very common. Sometime while modifying them after the respective iterators have been created, they throw java.lang.CurrentModificationException that is very critical to avoid.


In the above code, I can’t add or remove elements after creating the Iterator it. Doing so throws java.lang.CurrentModificationException. How to avoid it effectively?


Next I have used ListIterator<String> named listIterator which throws respective exceptions as can be shown in the above code in comments. How can we avoid them and add & set elements effectively using the add() and set() methods respectively?


Also, adding elements to the listIterator through the statement listIterator.add("D"); displays the elements on top of the list. Why?

  • 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-27T00:08:00+00:00Added an answer on May 27, 2026 at 12:08 am

    You’re getting a ConcurrentModificationException because you’re modifying the underlying collection while trying to iterate over it. You can’t call remove on list inside a loop where you’re using an iterator on list.

    The Iterator interface, however, provides an optional remove method that can be used to remove an item from the list you’re iterating over. Calling it.remove(); inside your loop will remove the last element returned from the iterator.

    The same applies for the ListIterator.

    As for why the add method puts things on “top of the list”, check out the documentation. What is happening is that the new element is being put into the underlying list directly in front of the element that would be returned on the next call to next. So on your next iteration through the loop, when you call next you will get the element you just inserted.

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

Sidebar

Related Questions

Following is a code that will encrypts the user String : import java.io.UnsupportedEncodingException; import
Following is some obviously-defective code for which I think the compiler should emit a
following code is used to find url from a string with php. Here is
I keep getting Clang errors on the following type of code and I can't
I am using MenuBuilder with code like this: public static MenuItem MainMenu(UrlHelper url) {
Having the following code: template<typename T, typename OutStream = std::ostream> struct print { OutStream
I have the following typedef's in my code: typedef unsigned long int ulint; typedef
I have a class that inherits ActiveRecordValidationBase that contains the following property: [Property] [ValidateDecimal]
At runtime an application (based on Java/Spring/Hibernate) throws the following exception: 07:18:38.511 ERROR Error
The MDN JavaScript guide states the following when doing a comparison between Java and

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.