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


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

Re: looping through a list, starting at 1

From Arved Sandstrom <asandstrom3minus1@eastlink.ca>
Newsgroups comp.lang.java.programmer
Subject Re: looping through a list, starting at 1
References <list-20110802003845@ram.dialup.fu-berlin.de> <ktqdna1VDImdrqrTnZ2dnUVZ_uydnZ2d@earthlink.com>
Message-ID <svHZp.78913$5v5.51956@newsfe11.iad> (permalink)
Organization Public Usenet Newsgroup Access
Date 2011-08-01 21:27 -0300

Show all headers | View raw


On 11-08-01 08:13 PM, Patricia Shanahan wrote:
> On 8/1/2011 3: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 ); ... }
>>
>>    ?
>>
> 
> This is the sort of situation in which I try writing a comment, even if
> I don't leave it in the code. The form that makes the comment easier to
> write is the one that better models programmer intent.
> 
> Patricia

It's a good idea. As it happens I think you'd end up with a comment no
matter what, simply to explain why you're skipping the first element.

I prefer the sublist-based foreach loop out of the 3 options presented
by Stefan and Andreas: both it and the standard 'for' loop highlight the
start index as a number, but the foreach more clearly emphasizes the
range being operated on. IMO.

AHS

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


Thread

Re: looping through a list, starting at 1 Patricia Shanahan <pats@acm.org> - 2011-08-01 16:13 -0700
  Re: looping through a list, starting at 1 Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-08-01 21:27 -0300

csiph-web