• Post Reply Bookmark Topic Watch Topic
  • New Topic

Concurrent Modification Exception In ArrayList  RSS feed

 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

My code is like this,



Its a simple program. I've list with some names as values like,

.

Now, I want to add "Hello" to each name by running above code. While iterating, first time I get Ram, I want to add "Hello" to Ram and add to list as "Ram Hello" also I want to remove original Value i.e "Ram". When I run above code it is giving Concurrent Modification Exception. How to do this?

Thanks:
Ramakrishna K.C
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you need to add the modified strings to a second list and replace the original list's content (or the target of the reference) with this second list.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you can't directly manipulate the value of the objects due to immutability of String, I'd say your best option is to use the set() method of a ListIterator:

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramakrishna Udupa wrote:
My code is like this,




Basically, you are not allowed to change the list (without using the iterator) while you are iterating. If you want to change the list while iterating, you need to do it with the iterator (and not with the list).

Ramakrishna Udupa wrote:
Its a simple program. I've list with some names as values like,

.

Now, I want to add "Hello" to each name by running above code. While iterating, first time I get Ram, I want to add "Hello" to Ram and add to list as "Ram Hello" also I want to remove original Value i.e "Ram". When I run above code it is giving Concurrent Modification Exception. How to do this?


Instead of using the add() and remove() methods, perhaps using the set() method (which does replacement) is a better option. Also, it would be a good idea to use a java.util.ListIterator instead, as that iterator as the methods that you need.

[EDIT: and I got beaten to the answer by three minutes]

Henry
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to replace "Suraj" with "Hello Suraj", you should not use add. Nor should you use an Iterator. You should iterate the List with an ordinary for loop and use the set method. As you will find here, that technique works well with arrayed lists, but may be slow for a linked list.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Nor should you use an Iterator...

Hmmm. Not so sure I agree with that (providing, as the others said, you use a ListIterator), because it will highlight any concurrent update.
Also, the technique you describe is likely to be much slower for linked lists, whereas using an iterator for an array-based List will likely only be marginally slower than using a loop.

Winston
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I never realised ListIterator has a set() method. You are right Winston, but I was beaten to the post twice within 4 minutes
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!