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!glegroupsg2000goo.googlegroups.com!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: iteration blues Date: Thu, 3 Nov 2011 09:32:07 -0700 (PDT) Organization: http://groups.google.com Lines: 89 Message-ID: <17339425.78.1320337927868.JavaMail.geo-discussion-forums@prms22> References: Reply-To: comp.lang.java.programmer@googlegroups.com NNTP-Posting-Host: 65.50.217.124 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: posting.google.com 1320337928 31297 127.0.0.1 (3 Nov 2011 16:32:08 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 3 Nov 2011 16:32:08 +0000 (UTC) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=65.50.217.124; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T User-Agent: G2/1.0 X-Google-Web-Client: true Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:9453 Knute Johnson 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 { >> static Vector particles = new Vector(); PLEASE DO NOT INDENT WITH THE TAB CHARACTER FOR USENET! >> 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 Why use 'Vector', then? Isn't 'ArrayList' available in that environment? >> 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. Not if you use the iterator, but OTOH removing things from the middle of a large list can be slow, depending on the list implementation. Wouldn't a 'Set' work for this? > 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. The collections Javadocs indicate the big-O for different operations, generally. Pick one that has characteristics that match your use case. I doubt that the synchronization inherent in 'Vector' will tie up too much time, but there's no noeed for it, is there? Aren't other 'List' implementations available? In the desktop world 'Vector' has been obsolete since 1998. 1998! Since Java 1.2! Thirteen years! That's 91 years in software years ("dog years"). -- Lew