Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Knute Johnson Newsgroups: comp.lang.java.programmer Subject: Re: iteration blues Date: Thu, 03 Nov 2011 08:51:18 -0700 Organization: A noiseless patient Spider Lines: 73 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 3 Nov 2011 15:51:11 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="mz/LDSJwiWnk3Jnnqg7x+Q"; logging-data="1764"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Ojou7y3xg8sciAzuZnf4B" User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20110929 Thunderbird/7.0.1 In-Reply-To: Cancel-Lock: sha1:9mbdsk6k3Q9wWleWjd0NIBFFN40= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:9451 On 11/3/2011 8:37 AM, 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 { > static Vector particles = new Vector(); > > public static void startfire(float x, float y) { > for (int ctr = 0; ctr< 100; ctr++) { > Particle p = new Particle(); > p.x = (float) (x + Math.random()-.5); > p.y = (float) (y + Math.random()-.5); > p.dx = (float) (Math.random()-.5)/4f; > p.dy = (float) (Math.random()-.5)/4f; > p.timeleft = (int) (Math.random() * 50 + 50); > particles.add(p); > } > } > > public static void burnfire() { > Iterator i = particles.iterator(); > Vector removelist = new Vector(); > while (i.hasNext()) { > Particle p = i.next(); > p.move(); > p.timeleft--; > if (p.timeleft == 0) removelist.add(p); > > } > particles.removeAll(removelist); > > } > > public static void drawfire(GL10 gl) { > Iterator i = particles.iterator(); > while (i.hasNext()) { > Particle p = i.next(); > p.draw(gl); > } > } > > } > > I'm concerned about inefficiency in the burnfire function. Does > anyone know how to rewrite this quickly if particles was a linked > list? The main issue is that I'm not sure if removing items during > iteration messes up the iterator. while (i.hasNext()) { > Particle p = i.next(); > p.move(); > p.timeleft--; while (--p.timeleft >= 0) p.remove(); > if (p.timeleft == 0) removelist.add(p); I have a game at http://rabbitbrush.frazmtn.com/asteroids.html that demonstrates this code. I think it is plenty quick for the type of animation that is being done. See the source code on the link at the bottom of the page. -- Knute Johnson