Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!nx02.iad01.newshosting.com!newshosting.com!news-out.readnews.com!transit3.readnews.com!postnews.google.com!q13g2000vbd.googlegroups.com!not-for-mail From: bob Newsgroups: comp.lang.java.programmer Subject: Re: iteration blues Date: Fri, 4 Nov 2011 13:42:58 -0700 (PDT) Organization: http://groups.google.com Lines: 104 Message-ID: <1d1f6dca-fa6a-4ced-8caa-b2d44c4a141b@q13g2000vbd.googlegroups.com> References: <9hhpttFi6sU1@mid.individual.net> NNTP-Posting-Host: 99.152.153.161 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1320439379 1334 127.0.0.1 (4 Nov 2011 20:42:59 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Fri, 4 Nov 2011 20:42:59 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: q13g2000vbd.googlegroups.com; posting-host=99.152.153.161; posting-account=v1lx5wkAAAALWYfGBkwkMb2guPF9cW2u User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-Header-Order: HUALESNKRC X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:7.0.1) Gecko/20100101 Firefox/7.0.1,gzip(gfe) Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:9526 On Nov 4, 2:34=A0am, Robert Klemme wrote: > On 11/03/2011 04:37 PM, 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 =A0static Vector =A0particles =3D new Vector(); > > > =A0 =A0public static void startfire(float x, float y) { > > =A0 =A0 =A0 =A0 =A0 =A0for (int ctr =3D 0; ctr< =A0100; ctr++) { > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Particle p =3D new Particle(); > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0p.x =3D (float) (x + Math.random= ()-.5); > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0p.y =3D (float) (y + Math.random= ()-.5); > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0p.dx =3D (float) (Math.random()-= .5)/4f; > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0p.dy =3D (float) (Math.random()-= .5)/4f; > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0p.timeleft =3D (int) (Math.rando= m() * 50 + 50); > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0particles.add(p); > > =A0 =A0 =A0 =A0 =A0 =A0} > > =A0 =A0} > > > =A0 =A0public static void burnfire() { > > =A0 =A0 =A0 =A0 =A0 =A0Iterator =A0i =3D particles.iterator()= ; > > =A0 =A0 =A0 =A0 =A0 =A0Vector =A0removelist =3D new Vector(); > > =A0 =A0 =A0 =A0 =A0 =A0while (i.hasNext()) { > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Particle p =3D i.next(); > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0p.move(); > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0p.timeleft--; > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0if (p.timeleft =3D=3D 0) removel= ist.add(p); > > > =A0 =A0 =A0 =A0 =A0 =A0} > > =A0 =A0 =A0 =A0 =A0 =A0particles.removeAll(removelist); > > > =A0 =A0} > > > =A0 =A0public static void drawfire(GL10 gl) { > > =A0 =A0 =A0 =A0 =A0 =A0Iterator =A0i =3D particles.iterator()= ; > > =A0 =A0 =A0 =A0 =A0 =A0while (i.hasNext()) { > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Particle p =3D i.next(); > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0p.draw(gl); > > =A0 =A0 =A0 =A0 =A0 =A0} > > =A0 =A0} > > > } > > > I'm concerned about inefficiency in the burnfire function. =A0Does > > 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. > > I'm surprised nobody seems to mention Iterator.remove(). > > public static void burnfire() { > =A0 =A0for (final Iterator i =3D particles.iterator(); i.hasNex= t();) { > =A0 =A0 =A0final Particle p =3D i.next(); > =A0 =A0 =A0p.move(); > =A0 =A0 =A0p.timeleft--; =A0// Direct access to member, bad! > > =A0 =A0 =A0if (p.timeleft =3D=3D 0) { > =A0 =A0 =A0 =A0iter.remove(); > =A0 =A0 =A0} > =A0 =A0} > > } > > This can be used regardless of container type. =A0Efficiency depends on > the ratio of removed elements. =A0If you remove much and do not need > indexed access (i.e. via List.get(int)) you can use a LinkedList. > Otherwise use ArrayList as indicated already. =A0There is no point in > using Vector these days any more. > > And btw, do not be concerned about performance, measure it. =A0Results ma= y > be surprising. > > Kind regards > > =A0 =A0 =A0 =A0 robert Just what I needed. Thanks.