[ImageJ-devel] Simplifying loops?

Stephan Saalfeld saalfeld at mpi-cbg.de
Mon Jun 14 16:43:19 CDT 2010


It should throw java.util.NoSuchElementException as defined by
java.util.Iterator (it's by the way java.util.Iterator and
java.lang.Iterable, lazy Eclipsers do not keep that stuff in mind ;))
Currently, it throws any kind of RuntimeException depending on what the
type() method is throwing (so usually a
java.lang.ArrayIndexOutOfBoundsException).

Best,
Stephan


On Mon, 2010-06-14 at 13:58 -0500, Brian Selinsky wrote: 
> What happens if next() is called without checking hasNest() first?
> 
> Would returning null or throwing an exception that could be caught and potentially ignored be reasonable?
> 
> 
> 
> On 06/14/10, Stephan Saalfeld  <saalfeld at mpi-cbg.de> wrote:
> 
> > Hi,
> > 
> > we implement java.lang.Iterator<T extends Type<T>> where next() returns
> > T, so no, next() cannot return boolean, fwd() and back() might do that.
> > In the coming changes, Image<T> implements java.lang.Iterable<T>, such
> > that the Java language shortcut works:
> > 
> > Image< T > image;
> > for ( final T : image ) {
> >     // meat of the loop
> > }
> > 
> > How's that?
> > 
> > Currently, Cursor<T> implements Iterable<T> such that you can do:
> > 
> > Cursor<T> cursor;
> > for (final T : cursor ) ...
> > 
> > instead,  but that's less sensible and will not stay.
> > 
> > Best,
> > Stephan
> > 
> > 
> > 
> > 
> > On Mon, 2010-06-14 at 18:54 +0200, Johannes Schindelin wrote: 
> > > Hi,
> > > 
> > > On Mon, 14 Jun 2010, Stephan Preibisch wrote:
> > > 
> > > > This is definitely something we could do as far as the linked iterators
> > > > work. Right now we did not do that because it is quite often extra work,
> > > > e.g. if you copy an image you need only one check instead of two:
> > > > 
> > > > cursor1, cursor2;
> > > > 
> > > > while ( cursor1.hasNext() ) 
> > > > {
> > > > 	cursor1.fwd();
> > > > 	cursor2.fwd();
> > > > 	// meat of the loop
> > > > }
> > > 
> > > Ah, I see. Maybe just a shortcut
> > > 
> > > 	public boolean next() {
> > >         	if (!hasNext())
> > >                 	return false;
> > >         	fwd();
> > >         	return true;
> > > 	}
> > > 
> > > to optimize for the common case?
> > > 
> > > Ciao,
> > > Dscho
> > > 
> > 
> > 
> > _______________________________________________
> > ImageJ-devel mailing list
> > ImageJ-devel at imagejdev.org
> > http://imagejdev.org/mailman/listinfo/imagej-devel





More information about the ImageJ-devel mailing list