Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > mono-thread concurrent modification

Reply
Thread Tools

mono-thread concurrent modification

 
 
jpprost@gmail.com
Guest
Posts: n/a
 
      11-29-2005
Hi,

I can't find an implementation of Collection which would let me legally
add new elements to the very list I iterate on. Let me re-phrase: I
want to iterate on an array/list (or whatever structure which is
Collection-compatible) and in the meantime add new elements to this
list (say at the end), so that they are taken into account in the
iteration process. It's important to emphasize that I only need to add
new elements--no removal is required.
- A SynchronizedList doesn't help here, since my application is
mono-thread--and anyway even when faking a synchronized section I still
get a ConcurrentModificationException.
- I thought a ListIterator with a LinkedList would be the solution, but
no: I still get the same exception--though in theory I don't see the
problem: I should be allowed to add new elements at the end of a linked
list on which I iterate, shouldn't I?...

Do I really have to implement something myself, or should I have a
better look somewhere?

Cheers,

--
Jean-Philippe Prost [E6A 341, x9542]
Centre for Language Technology
Macquarie University ~ Sydney, Australia
<http://www.ics.mq.edu.au/~jpprost/>
_______________________________________________

 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      11-29-2005
On 28 Nov 2005 22:12:03 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote, quoted or
indirectly quoted someone who said :

>Let me re-phrase: I
>want to iterate on an array/list (or whatever structure which is
>Collection-compatible) and in the meantime add new elements to this
>list (say at the end),


look at the methods of Iterator.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      11-29-2005
On Tue, 29 Nov 2005 06:24:09 GMT, Roedy Green
<(E-Mail Removed) > wrote, quoted or
indirectly quoted someone who said :

>
>look at the methods of Iterator.


sorry ListIterator.add. It inserts at the spot you are working on.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      11-29-2005

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Hi,
>
> I can't find an implementation of Collection which would let me legally
> add new elements to the very list I iterate on. Let me re-phrase: I
> want to iterate on an array/list (or whatever structure which is
> Collection-compatible) and in the meantime add new elements to this
> list (say at the end), so that they are taken into account in the
> iteration process. It's important to emphasize that I only need to add
> new elements--no removal is required.


If it's a list, why not just use an integer index to step through it; this
works fine when elements are added to the end. Just remember to compare it
to the current size() rather than the size() when the iteration started.


 
Reply With Quote
 
jpprost@gmail.com
Guest
Posts: n/a
 
      11-29-2005
Yes, that's what I thought but for some reason I still get the same
exception.

--
Jean-Philippe Prost [E6A 341, x9542]
Centre for Language Technology
Macquarie University ~ Sydney, Australia
<http://www.ics.mq.edu.au/~jpprost/>
_______________________________________________

 
Reply With Quote
 
LaieTechie
Guest
Posts: n/a
 
      11-29-2005
On Tue, 29 Nov 2005 06:28:36 +0000, Mike Schilling wrote:

> If it's a list, why not just use an integer index to step through it; this
> works fine when elements are added to the end. Just remember to compare
> it to the current size() rather than the size() when the iteration
> started.


If it's a LinkedList, then by supplying an index, it has to walk from the
beginning - not very efficient.

Laie Techie

 
Reply With Quote
 
LaieTechie
Guest
Posts: n/a
 
      11-29-2005
On Tue, 29 Nov 2005 06:28:36 +0000, Mike Schilling wrote:

> If it's a list, why not just use an integer index to step through it; this
> works fine when elements are added to the end. Just remember to compare
> it to the current size() rather than the size() when the iteration
> started.


If it's a LinkedList, then by supplying an index, it has to walk from the
beginning - not very efficient.

Laie Techie

 
Reply With Quote
 
zero
Guest
Posts: n/a
 
      11-29-2005
(E-Mail Removed) wrote in news:1133246093.516969.140790
@g49g2000cwa.googlegroups.com:

> Yes, that's what I thought but for some reason I still get the same
> exception.
>


What's the exact exception, and the code that produces it - would be
helpful if we could see the actual line that throws the exception, and some
code leading up to it.


--
Beware the False Authority Syndrome
 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      11-29-2005
Mike Schilling wrote:

> If it's a list, why not just use an integer index to step through it; this
> works fine when elements are added to the end. Just remember to compare
> it to the current size() rather than the size() when the iteration
> started.


And also remember to change the LinkedList (as in the OP's experiment) back to
an ArrayList !

-- chris


 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      11-29-2005

"Chris Uppal" <(E-Mail Removed)-THIS.org> wrote in message
news:438c39d0$1$20532$(E-Mail Removed).. .
> Mike Schilling wrote:
>
>> If it's a list, why not just use an integer index to step through it;
>> this
>> works fine when elements are added to the end. Just remember to compare
>> it to the current size() rather than the size() when the iteration
>> started.

>
> And also remember to change the LinkedList (as in the OP's experiment)
> back to
> an ArrayList !


Only if performance is an issue

To be honest, I'm not sure why a LinkedList isn't a LinkedCollection, since
performing random-access operations on one is a bad idea. (Or conversely,
why there isn't a RandomAcesssList interface that adds those methods, while
List merely adds the listIterator() method).


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problems locating the concurrent EDU.oswego.cs.dl.util.concurrent package Pep Java 6 08-16-2005 07:26 AM
DNS Reply Modification (doctoring) intermittently failing Dav0 Cisco 1 01-28-2005 07:00 PM
Modification of Duty Cycle - Possible? Drew VHDL 2 08-02-2004 06:54 PM
Schema Modification Error Calum Michie MCSE 1 12-12-2003 03:27 PM
modification : where can i find MOC ? bruno MCSE 3 09-19-2003 11:27 AM



Advertisments