Solved

How to handle ConcurrentModificationException

Posted on 2009-07-15
3
311 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

733 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