• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Tim Cooke
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
Saloon Keepers:
  • Piet Souris
Bartenders:

Concurrent Modification Exception In ArrayList

 
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 161
Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
author
Posts: 23965
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 82459
594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 82459
594
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic