Solved

How to handle ConcurrentModificationException

Posted on 2009-07-15
3
305 Views
Last Modified: 2013-11-23
As obvious, the attached code  is a recipe of ConcurrentModificationException. But how to handle when you need to make changes i.e. remove some items from list while iterating over.
List<String> strList = new ArrayList<String>();

		strList.add("A");

		strList.add("B");

		strList.add("C");

		strList.add("D");

		strList.add("E");

		strList.add("F");

		strList.add("G");

		strList.add("H");

		

		for (String str : strList) {

			if (str.equals("E"))

				strList.remove(str);

			System.err.println(str);

		}

Open in new window

0
Comment
Question by:fix2009
3 Comments
 
LVL 14

Accepted Solution

by:
CPColin earned 225 total points
Comment Utility
You have to use an Iterator, then use its remove() method. It's a slight drawback of the enhanced for loop feature.
0
 
LVL 13

Assisted Solution

by:Bart_Cr
Bart_Cr earned 125 total points
Comment Utility
You should work with a real iterator
List<String> strList = new ArrayList<String>();

strList.add("A");

strList.add("B");

strList.add("C");

strList.add("D");

strList.add("E");

strList.add("F");

strList.add("G");

strList.add("H");
 

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

while (it.hasNext()) {

    String str = it.next();            

    if (str.equals("E"))

        it.remove();

        System.err.println(str);

    }

}

Open in new window

0
 

Author Closing Comment

by:fix2009
Comment Utility
Thanks Guys.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now