Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #9464

Re: iteration blues

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 <lewbloch@gmail.com>
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> (permalink)
References <a84ab4cf-a960-4783-a955-0718438dab63@bq8g2000vbb.googlegroups.com> <d78a109c-f5c9-4528-9806-1a4680e8a225@r7g2000vbg.googlegroups.com>
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 <d78a109c-f5c9-4528-9806-1a4680e8a225@r7g2000vbg.googlegroups.com>
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

Show key headers only | View raw


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 {
>>         static Vector<Particle> particles = new Vector<Particle>();
>> ... [snip]
>>         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?  What do your measurements tell you?  What is not working because of the time this method takes?

>> 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.
> 
> 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.
> 
> 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);
>     }
>   }
> 
> No need to expose the iterator anymore...

That is not true.  There are all kinds of scenarios that require one to expose the iterator.  It looks like the OP's scenario, for one, requires him to 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 concurrency issues yet.

-- 
Lew

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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