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


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

Re: looping through a list, starting at 1

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!nx02.iad01.newshosting.com!newshosting.com!news-out.readnews.com!transit3.readnews.com!postnews.google.com!news4.google.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Chris Riesbeck <Chris.Riesbeck@gmail.com>
Newsgroups comp.lang.java.programmer
Subject Re: looping through a list, starting at 1
Date Tue, 02 Aug 2011 14:38:58 -0500
Lines 58
Message-ID <99r22jF3t1U1@mid.individual.net> (permalink)
References <list-20110802003845@ram.dialup.fu-berlin.de> <j17l8r$1p2$1@dont-email.me>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 8bit
X-Trace individual.net OU0cgqeyrLEraqY7JXcJZgKLOib8HOUeM83OVfe6mW8BOQicQi
Cancel-Lock sha1:Aqq30Dof8Y6CTOZVveUPdvfD3Kk=
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20110624 Thunderbird/5.0
In-Reply-To <j17l8r$1p2$1@dont-email.me>
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:6760

Show key headers only | View raw


On 8/1/2011 8:50 PM, Eric Sosman wrote:
> On 8/1/2011 6:45 PM, Stefan Ram wrote:
>> Assuming a list has a sufficient number of entries at run
>> time, what should be prefered to assign a reference to each
>> entry to »e«, starting at index 1:
>>
>> for( final E e : l.sublist( 1, l.size() ))...
>>
>> or
>>
>> for( int i = 1; i< l.size(); ++i ){ final E e = l.get( 0 ); ... }
>
> (ITYM l.get(i)?)
>
> How about
>
> Iterator<E> it = l.iterator();
> it.next(); // ignore element 0
> while (it.hasNext()) {
> E e = it.next();
> ...
> }
>
> In short, there may well be half-a-dozen ways to do what you ask,
> if not more. None of them stands out as "preferred" to my eye;
> you may as well do whatever seems natural.

To add yet another option, if you are writing lots of iterations over 
the "tail" of a collection, then you could define an Iterable wrapping 
class, like this:

public class Tail<T> implements Iterable<T> {

   public static <T> Tail<T> tail(final Iterable<T> coll) {
     return new Tail<T>(coll);
   }

   public Iterator<T> iterator() {
     return iter;
   }

   private Iterator<T> iter;

   private Tail(final Iterable<T> coll) {
     iter = coll.iterator();
     if (iter.hasNext()) iter.next();
   }
}

then to use

   import static utils.Tail.tail;

   ...

   for (final E e : tail( list )) {
      ...
   }

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


Thread

Re: looping through a list, starting at 1 Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-08-01 21:50 -0400
  Re: looping through a list, starting at 1 Patricia Shanahan <pats@acm.org> - 2011-08-01 20:34 -0700
    Re: looping through a list, starting at 1 Patricia Shanahan <pats@acm.org> - 2011-08-01 20:35 -0700
  Re: looping through a list, starting at 1 Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-08-02 07:31 -0400
  Re: looping through a list, starting at 1 Chris Riesbeck <Chris.Riesbeck@gmail.com> - 2011-08-02 14:38 -0500
    Re: looping through a list, starting at 1 markspace <-@.> - 2011-08-02 12:50 -0700
      Re: looping through a list, starting at 1 Chris Riesbeck <Chris.Riesbeck@gmail.com> - 2011-08-03 13:34 -0500

csiph-web