Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #9453
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: iteration blues |
| Date | 2011-11-03 09:32 -0700 |
| Organization | http://groups.google.com |
| Message-ID | <17339425.78.1320337927868.JavaMail.geo-discussion-forums@prms22> (permalink) |
| References | <a84ab4cf-a960-4783-a955-0718438dab63@bq8g2000vbb.googlegroups.com> <j8ud9f$1n4$1@dont-email.me> |
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<Particle> particles = new Vector<Particle>();
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<Particle> i = particles.iterator();
>> Vector<Particle> removelist = new Vector<Particle>();
>> 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<Particle> 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
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
iteration blues bob <bob@coolgroups.com> - 2011-11-03 08:37 -0700
Re: iteration blues Knute Johnson <nospam@knutejohnson.com> - 2011-11-03 08:51 -0700
Re: iteration blues Lew <lewbloch@gmail.com> - 2011-11-03 09:32 -0700
Re: iteration blues Arne Vajhøj <arne@vajhoej.dk> - 2011-11-04 21:00 -0400
Re: iteration blues spk <jhic@speak.invalid> - 2011-11-05 07:48 -0400
Re: iteration blues Henk van Voorthuijsen <voorth@xs4all.nl> - 2011-11-03 09:31 -0700
Re: iteration blues Lew <lewbloch@gmail.com> - 2011-11-03 13:50 -0700
Re: iteration blues Henk van Voorthuijsen <voorth@xs4all.nl> - 2011-11-04 08:07 -0700
Re: iteration blues Arne Vajhøj <arne@vajhoej.dk> - 2011-11-04 21:02 -0400
Re: iteration blues Roedy Green <see_website@mindprod.com.invalid> - 2011-11-03 10:08 -0700
Re: iteration blues Travers Naran <tnaran@gmail.com> - 2011-11-03 22:22 -0700
Re: iteration blues Robert Klemme <shortcutter@googlemail.com> - 2011-11-04 10:34 +0100
Re: iteration blues Lew <lewbloch@gmail.com> - 2011-11-04 10:46 -0700
Re: iteration blues Robert Klemme <shortcutter@googlemail.com> - 2011-11-04 23:55 +0100
Re: iteration blues Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-11-04 21:06 -0400
Re: iteration blues Lew <lewbloch@gmail.com> - 2011-11-04 20:30 -0700
Re: iteration blues bob <bob@coolgroups.com> - 2011-11-05 12:40 -0700
Re: iteration blues Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-11-05 16:14 -0400
Re: iteration blues Lew <lewbloch@gmail.com> - 2011-11-05 13:41 -0700
Re: iteration blues bob <bob@coolgroups.com> - 2011-11-04 13:42 -0700
csiph-web