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


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

Re: looping through a list, starting at 1

From Raymond Tong <raytong82@gmail.com>
Newsgroups comp.lang.java.programmer
Subject Re: looping through a list, starting at 1
Date 2011-08-05 02:04 -0700
Organization http://groups.google.com
Message-ID <22144c22-58ed-4498-b8ff-7c1cdadccbe4@b34g2000yqi.googlegroups.com> (permalink)
References <list-20110802003845@ram.dialup.fu-berlin.de>

Show all headers | View raw


If the List has implemented RandomAccess interface, the latter
approach (i.e. calling get(i)) is preferred.
Otherwise, the former (i.e. getting the iterator) is preferred.

e.g. ArrayList implemented RandomAccess which backed by Array.
Calling get(i) simply return the array element. But has little
overhead on creating the Iterator instance.

e.g. LinkedList does NOT implement RandomAccess
Calling get(i) needed to iterate from the first node until (i)th
element.
But create the Iterator once would same time for a large size list.

In conclusion, depeneds on the implementation of your list.
if (list instanceof RandomAccess) {
  // latter
} else {
  // former
}

Back to comp.lang.java.programmer | Previous | Next | Find similar


Thread

Re: looping through a list, starting at 1 Raymond Tong <raytong82@gmail.com> - 2011-08-05 02:04 -0700

csiph-web