On Tue, Nov 29, 2011 at 10:31 AM, bob <b...@coolgroups.com> wrote: > I'm looking at the Best Practices section of "Beginning Android > Games", and it says: > > "Don't use iterators, as they create new objects." > > Can someone help me understand? >
Using iterators creates new heap allocated "Iterator" objects, where you can sometimes use indices as integers to iterate through arrays, etc... Dan Bornstien's talk "dalvik vm internals" contains an explanation of this and a scale from "good" to "bad" in terms of this, you can google for the video / slides. > The reason I ask is because I'm seeing performance issues with this > method: > > public static void burnfire() { > for (final Iterator<Particle> i = particles.iterator(); > i.hasNext();) { > final Particle p = i.next(); > p.move(); > p.timeleft--; > > if (p.timeleft == 0) { > i.remove(); > } > } > > } > > The method just traverses a linked list and moves some particles. Are > objects being created there? > yup. An Iterator object. > How can I make that faster? > You can use an int instead, if it's an array. kris -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en