Solved

How to handle ConcurrentModificationException

Posted on 2009-07-15
3
307 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
ID: 24863196
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 Cremers
Bart Cremers earned 125 total points
ID: 24863211
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
ID: 31603929
Thanks Guys.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

867 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

24 Experts available now in Live!

Get 1:1 Help Now