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


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

Re: looping through a list, starting at 1

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail
From Eric Sosman <esosman@ieee-dot-org.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: looping through a list, starting at 1
Date Mon, 01 Aug 2011 21:50:59 -0400
Organization A noiseless patient Spider
Lines 40
Message-ID <j17l8r$1p2$1@dont-email.me> (permalink)
References <list-20110802003845@ram.dialup.fu-berlin.de>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 8bit
Injection-Date Tue, 2 Aug 2011 01:52:27 +0000 (UTC)
Injection-Info mx04.eternal-september.org; posting-host="f8igmItKsWs6nM5YanFxAA"; logging-data="1826"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/3pPFpsy4pdasg8ts31MA6"
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20110624 Thunderbird/5.0
In-Reply-To <list-20110802003845@ram.dialup.fu-berlin.de>
Cancel-Lock sha1:t3YxW+1y9Y1Tcx4sXLmHMBa7+zI=
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:6728

Show key headers only | View raw


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.

     ... and "natural" is a little unnatural, it seems to me.  If
the various E are truly independent -- if l is merely a Collection
for the purposes of the loop -- one wonders where the interloper at
position 0 came from.  And if the position really matters -- maybe
you're looking at adjacent pairs or something -- then clearly i has
more significance than a purely synthetic iteration control would
(hence your second form would be preferred, because somewhere in the
body you'd be doing l.get(i-1).)  As a problem in the abstract I see
no clear reason to choose one form over its peers; with a concrete
context I might.

-- 
Eric Sosman
esosman@ieee-dot-org.invalid

Back to comp.lang.java.programmer | Previous | NextNext 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