Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: v_borchert@despammed.com (Volker Borchert) Newsgroups: comp.lang.java.programmer Subject: Re: looping through a list, starting at 1 Date: 2 Aug 2011 19:32:44 GMT Organization: Private site at Eddersheim, Germany Lines: 29 Distribution: world Message-ID: References: <4e3745a2$0$305$14726298@news.sunsite.dk> <62239393-929c-4764-8c8e-9620a03a7b81@c29g2000yqd.googlegroups.com> X-Trace: individual.net jZEekj7k9M233MdVs+lFTgzN2egj1VIau6PvBN4O+pT4ntD1Jc Cancel-Lock: sha1:1IRMCxaARmdOOpQJdMtUF54TKdY= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:6762 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 "I'm a mechanic, not a doctor." Volker Borchert