Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try looping using an <code>Iterator</code>, since per Oracle <code>Iterator.remove()</code> is the only safe way to remove an item from a <code>Collection</code> (including a <code>Stack</code>) during iteration.</p> <p>From <a href="http://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html" rel="nofollow">http://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html</a></p> <blockquote> <p>Note that Iterator.remove is the only safe way to modify a collection during iteration; the behavior is unspecified if the underlying collection is modified in any other way while the iteration is in progress.</p> </blockquote> <p>So something like the following should work:</p> <pre><code>Stack&lt;Particle&gt; particles = new Stack&lt;Particle&gt;(); ... // Add a bunch of particles Iterator&lt;Particle&gt; iter = particles.iterator(); while (iter.hasNext()) { Particle p = iter.next(); if (!p.isAlive()) { iter.remove(); } } </code></pre> <p>I've used this approach in a real Android app (<a href="https://play.google.com/store/apps/details?id=com.joulespersecond.seattlebusbot" rel="nofollow">OneBusAway Android</a> - see code <a href="https://github.com/OneBusAway/onebusaway-android/blob/35fbc9549dc5bff8ff1c0c0bc71274b4d6d13c20/src/com/joulespersecond/seattlebusbot/RegionsFragment.java#L124" rel="nofollow">here</a>), and it worked for me. Note that in the code for this app I also included a try/catch block in case the platform throws an exception, and in this case just iterate through a copy of the collection and then remove the item from the original collection.</p> <p>For you, this would look like:</p> <pre><code>try { ... // above code using iterator.remove } catch(UnsupportedOperationException e) { Log.w(TAG, "Problem removing from stack using iterator: " + e); // The platform apparently didn't like the efficient way to do this, so we'll just // loop through a copy and remove what we don't want from the original ArrayList&lt;Particle&gt; copy = new ArrayList&lt;Particle&gt;(particles); for (Particle p : copy) { if (!p.isAlive()) { particles.remove(p); } } } </code></pre> <p>This way you get the more efficient approach if the platform supports it, and if not you still have a backup.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload