Re: List or Iterator
Donkey Hottie wrote:
"Adam Lipscombe" <adam.lipscombe@qucs.co.uk> wrote in
message news:4A69C31C.5060508@qucs.co.uk
Point taken. thanks.
I was think more in terms of execution speed.
thanks - adam
I have a feeling that you pondereda about the execution speed of the
method call only.
All objects passed to a method are just the reference to the object.
There will be no difference in the method all if the object is an
ArrayList or an Iterator. They are both Objects, and they have a similar
reference.
It seemed to me he was wondering about the difference of
passing a List to a method that would then create an Iterator
and traverse the List with it, versus passing a ready-made
Iterator in the first place. True, the method no longer need
do the work of creating the Iterator. But *somebody* must do
that work! If the caller does it, the work has merely been
moved from one location to another, not eliminated.
If the caller wanted to do another, independent traversal
of the same List, it is just barely within the outer limits
of possibility that it might be a skillionth of a milliquiver
faster to create an Iterator and clone() it (if it's Cloneable)
and use the copies than to create two Iterators. That's the
sort of thing I was thinking of when I wrote about differences
so small they'd be difficult to measure.
Earlier, I gave some examples of things you could do with
a List that you could not do with an Iterator. For balance,
I should point out that there are things an Iterator can do
that a List cannot, like retrieve elements of collections
other than Lists. If I want a method that "Visits all the
elements of a something-or-other," writing the method to take
an Iterator allows me to use it with Lists, Sets, BeanContexts,
or anything else that can provide an Iterator. The choice of
what to use should be driven primarily by what needs doing
(or what might someday need doing), and only secondarily by
concerns of performance. Doing wrong rapidly ain't right.
--
Eric Sosman
esosman@ieee-dot-org.invalid