Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > looping through a list, starting at 1

Reply
Thread Tools

looping through a list, starting at 1

 
 
Volker Borchert
Guest
Posts: n/a
 
      08-02-2011
Robert Klemme wrote:
> If there are performance issues then we can optimize later anyway.


In that case, I'd go for something like

if (l instanceof RandomAccess) {
for (int i = 1, n = l.size(); i < n; ++i) {
final E e = l.get(i);
// ...
}
} else {
final Iterator i = l.iterator();
if (i.hasNext()) {
i.next();
while (i.hasNext()) {
final E e = i.next();
// ...
}
}
}

and hope that all custom List implementations encountered truthfully
implement or not RandomAccess, and that those that do have a fast size()
and those that don't have a fast Iterator.hasNext().

--

"I'm a doctor, not a mechanic." Dr Leonard McCoy <(E-Mail Removed)>
"I'm a mechanic, not a doctor." Volker Borchert <(E-Mail Removed)>
 
Reply With Quote
 
 
 
 
Chris Riesbeck
Guest
Posts: n/a
 
      08-02-2011
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 )) {
...
}
 
Reply With Quote
 
 
 
 
markspace
Guest
Posts: n/a
 
      08-02-2011
On 8/2/2011 12:38 PM, Chris Riesbeck wrote:

> import static utils.Tail.tail;
>
> for (final E e : tail( list )) {
> ...
> }



I like the static factory idea, but why define a whole new class?

public class CollectionUtils {

public static <T> List<T> skip( int n, List<T> list ) {
return list.sublist( n, list.size() ) ;
}

public static <T> List<T> skip( List<T> list ) {
return skip( 1, list ) ;
}

....


 
Reply With Quote
 
Chris Riesbeck
Guest
Posts: n/a
 
      08-03-2011
On 8/2/2011 2:50 PM, markspace wrote:
> On 8/2/2011 12:38 PM, Chris Riesbeck wrote:
>
>> import static utils.Tail.tail;
>>
>> for (final E e : tail( list )) {
>> ...
>> }

>
>
> I like the static factory idea, but why define a whole new class?
>
> public class CollectionUtils {
>
> public static <T> List<T> skip( int n, List<T> list ) {
> return list.sublist( n, list.size() ) ;
> }
>
> public static <T> List<T> skip( List<T> list ) {
> return skip( 1, list ) ;
> }


Just generality ... Tail.tail() only requires something that's Iterable,
not a List.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      08-03-2011
On 1 Aug 2011 22:45:41 GMT, http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de (Stefan Ram) wrote,
quoted or indirectly quoted someone who said :

>for( int i = 1; i < l.size(); ++i ){ final E e = l.get( 0 ); ... }


This one is clearer. The problem is most people will just glance at it
and read it as for( int i=0; i<l.zise(); i++ )

I think you mean i++ not ++i.


you might use for ( E e : l )
Then use logic to avoid the first elt, though that is overkill for
ensuring noone misreads your code.
--
Roedy Green Canadian Mind Products
http://mindprod.com
Most of computer code is for telling the computer
what do if some very particular thing goes wrong.
 
Reply With Quote
 
Arved Sandstrom
Guest
Posts: n/a
 
      08-03-2011
On 11-08-03 07:40 PM, Roedy Green wrote:
> On 1 Aug 2011 22:45:41 GMT, (E-Mail Removed)-berlin.de (Stefan Ram) wrote,
> quoted or indirectly quoted someone who said :
>
>> for( int i = 1; i < l.size(); ++i ){ final E e = l.get( 0 ); ... }

>
> This one is clearer. The problem is most people will just glance at it
> and read it as for( int i=0; i<l.zise(); i++ )


Errr, I don't know if "most" people would misread the above, although no
doubt some would, but how can you assert that something is more clear
then immediately assert that most people will mis-read it?

> I think you mean i++ not ++i.


In this particular case why would it matter? Write a simple test case
that has 2 loops, one using pre-increment for the increment expression,
one using post-increment, and see if there's a difference. There won't
be: the increment expression is invoked *after* each iteration through
the loop.

> you might use for ( E e : l )
> Then use logic to avoid the first elt, though that is overkill for
> ensuring noone misreads your code.


AHS
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      08-04-2011
On 8/3/2011 6:40 PM, Roedy Green wrote:
> On 1 Aug 2011 22:45:41 GMT, (E-Mail Removed)-berlin.de (Stefan Ram) wrote,
> quoted or indirectly quoted someone who said :
>
>> for( int i = 1; i< l.size(); ++i ){ final E e = l.get( 0 ); ... }

>
> This one is clearer. The problem is most people will just glance at it
> and read it as for( int i=0; i<l.zise(); i++ )


Soundz! Can beingz unable to diztinguizh onez from seroez exizt
outzide of sooz?

> I think you mean i++ not ++i.


Say what? Or, rather, say why?

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
John B. Matthews
Guest
Posts: n/a
 
      08-04-2011
In article <(E-Mail Removed)>,
Roedy Green <(E-Mail Removed)> wrote:

> I think you mean i++ not ++i.



IIUC, the result of evaluating any ForUpdate expression is discarded,
so I'm not sure how the difference would matter.

<http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.14.1.2>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
 
Reply With Quote
 
Gene Wirchenko
Guest
Posts: n/a
 
      08-04-2011
On Wed, 03 Aug 2011 21:10:46 -0400, Eric Sosman
<(E-Mail Removed)> wrote:

>On 8/3/2011 6:40 PM, Roedy Green wrote:
>> On 1 Aug 2011 22:45:41 GMT, (E-Mail Removed)-berlin.de (Stefan Ram) wrote,
>> quoted or indirectly quoted someone who said :
>>
>>> for( int i = 1; i< l.size(); ++i ){ final E e = l.get( 0 ); ... }

>>
>> This one is clearer. The problem is most people will just glance at it
>> and read it as for( int i=0; i<l.zise(); i++ )

>
> Soundz! Can beingz unable to diztinguizh onez from seroez exizt
>outzide of sooz?


Mr. Zozman, pleaze do not make fun of Roedy.

>> I think you mean i++ not ++i.

>
> Say what? Or, rather, say why?


Add me to the list of those asking why.

Sincerely,

Gene Wirchenko
 
Reply With Quote
 
RedGrittyBrick
Guest
Posts: n/a
 
      08-04-2011
On 04/08/2011 04:36, John B. Matthews wrote:
> In article<(E-Mail Removed) >,
> Roedy Green<(E-Mail Removed)> wrote:
>
>> I think you mean i++ not ++i.

>
>
> IIUC, the result of evaluating any ForUpdate expression is discarded,
> so I'm not sure how the difference would matter.


Since I see postincrement a lot more than I see preincrement and since
i++ is idiomatic in that construct, when seeing ++i I tend to stop and
wonder whether the writer intended something else. So I suppose it might
matter if engendering a sense of distrust or unease in maintenance
programmers matters.

--
RGB
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
looping through json array loops through the characters instead ofthe values Aaron Javascript 2 04-10-2011 05:58 PM
looping through array starting at i !=0 John Smith Ruby 2 10-07-2010 10:58 AM
3 ESSENTIAL TOOLS FOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLSFOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLS FOR STARTING ANDMAINTAINING... Oanh Bui C++ 0 04-27-2009 12:51 PM
3 ESSENTIAL TOOLS FOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLSFOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLS FOR STARTING ANDMAINTAINING... Oanh Bui C Programming 0 04-27-2009 12:51 PM
3 ESSENTIAL TOOLS FOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLSFOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLS FOR STARTING ANDMAINTAINING... Oanh Bui Python 0 04-27-2009 12:46 PM



Advertisments