Solved

How to handle ConcurrentModificationException

Posted on 2009-07-15
3
313 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

696 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