Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #6803
| 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:11 -0700 |
| Organization | http://groups.google.com |
| Message-ID | <6c7f77ed-569f-47b0-a83a-931e4fa7a544@e7g2000vbw.googlegroups.com> (permalink) |
| References | <list-20110802003845@ram.dialup.fu-berlin.de> |
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 | Next — Next in thread | Find similar
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