Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.42!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder2-2.proxad.net!nx01.iad01.newshosting.com!newshosting.com!216.196.98.142.MISMATCH!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: iteration blues Date: Thu, 3 Nov 2011 13:50:06 -0700 (PDT) Organization: http://groups.google.com Lines: 75 Message-ID: <13953747.276.1320353406722.JavaMail.geo-discussion-forums@prog16> References: Reply-To: comp.lang.java.programmer@googlegroups.com NNTP-Posting-Host: 2620:0:1000:2404:224:d7ff:fe69:5838 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1320353406 28585 127.0.0.1 (3 Nov 2011 20:50:06 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 3 Nov 2011 20:50:06 +0000 (UTC) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=2620:0:1000:2404:224:d7ff:fe69:5838; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T User-Agent: G2/1.0 X-Google-Web-Client: true Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:9464 Henk van Voorthuijsen wrote: > bob wrote: >> So, I wrote this code for some particle effects: >> >> package com.coolfone.particles; >> >> import java.util.Iterator; >> import java.util.Vector; >> >> import javax.microedition.khronos.opengles.GL10; >> >> public class FireManager { >> =A0 =A0 =A0 =A0 static Vector particles =3D new Vector(); >> ... [snip] >> =A0 =A0 =A0 =A0 public static void drawfire(GL10 gl) { >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Iterator i =3D particles.itera= tor(); >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 while (i.hasNext()) { >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Particle p =3D i.next(); >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 p.draw(gl); >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 } >> =A0 =A0 =A0 =A0 } >> >> } >> >> I'm concerned about inefficiency in the burnfire function. =A0Does Why? What do your measurements tell you? What is not working because of t= he time this method takes? >> anyone know how to rewrite this quickly if particles was a linked >> list? =A0The main issue is that I'm not sure if removing items during > > iteration messes up the iterator. >=20 > all Vectors should be LinkedLists, I think. Since you're only adding > to the end of the list or terating over it, performance shouldn't be > an issue. >=20 > BTW, while loops over an iterator are obsolete since Java 1.5 came > out. > Consider using the enhanced for loop: > public static void drawfire(GL10 gl) { > for ( Particle p: particles ) { > p.draw(gl); > } > } >=20 > No need to expose the iterator anymore... That is not true. There are all kinds of scenarios that require one to exp= ose the iterator. It looks like the OP's scenario, for one, requires him t= o expose the iterator. the issue is removal of items from the list as each is processed. If your = algorithm is (pseudocoded): for each item in collection process item delete item from collection you will need the iterator. If your algorithm is: for each item in collection process items empty the collection then you will not need the iterator. Talking in a single-threaded world here. I'm not going to delve into concu= rrency issues yet. --=20 Lew