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


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

Re: looping through a list, starting at 1

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!74.125.46.80.MISMATCH!postnews.google.com!e7g2000vbw.googlegroups.com!not-for-mail
From Raymond Tong <raytong82@gmail.com>
Newsgroups comp.lang.java.programmer
Subject Re: looping through a list, starting at 1
Date Fri, 5 Aug 2011 02:11:13 -0700 (PDT)
Organization http://groups.google.com
Lines 21
Message-ID <6c7f77ed-569f-47b0-a83a-931e4fa7a544@e7g2000vbw.googlegroups.com> (permalink)
References <list-20110802003845@ram.dialup.fu-berlin.de>
NNTP-Posting-Host 63.217.84.228
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1
X-Trace posting.google.com 1312535473 7356 127.0.0.1 (5 Aug 2011 09:11:13 GMT)
X-Complaints-To groups-abuse@google.com
NNTP-Posting-Date Fri, 5 Aug 2011 09:11:13 +0000 (UTC)
Complaints-To groups-abuse@google.com
Injection-Info e7g2000vbw.googlegroups.com; posting-host=63.217.84.228; posting-account=a5J4BgoAAAD0-MG4jBM14khcJAzYXpW_
User-Agent G2/1.0
X-Google-Web-Client true
X-Google-Header-Order ARLUEHNKC
X-HTTP-UserAgent Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; MS-RTC LM 8),gzip(gfe)
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:6803

Show key headers only | View raw


It depends on the implmentation of list.
If the list implements RandomAccess interface, it is recommended to
call get(i) instead of using iterator().

e.g. ArrayList implements RandomAccess interface and backed by Array.
Calling get(i) would simply return (i)th element in an array which is
quick.
Creating an iterator has little overhead on creating an instance of
Iterator object.

e.g. LinkedList does NOT implement RandomAccess.
Calling get(i) need to look from the fist node to (i)th element which
is slow if i is large.
Creating an iterator is one time action which save time for sequential
lookup.

if (list instanceof RandomAccess) {
  // get(i)
} else {
  // iterator
}

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


Thread

Re: looping through a list, starting at 1 Raymond Tong <raytong82@gmail.com> - 2011-08-05 02:11 -0700
  Re: looping through a list, starting at 1 Patricia Shanahan <pats@acm.org> - 2011-08-05 10:39 -0700

csiph-web