
Signature
Knute Johnson
email s/nospam/knute/
On Nov 25, 9:50 pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
> All of them are much slower than using an array. If performance is
> really the issue then you don't want to use any of the Collection
> objects. But usually speed isn't the most important thing. And the
> collection classes are very easy to use.
To add to Knute's point, if you look up Vector's source code, the
get(int index) uses direct access to an array's index.
-cheers,
Manish
>>> Any performance advantage for traversing elements
>>>from list using Iterator over get(int index) ?
>> Depends on the list implementation, but generally an iterator is as fast or
>> faster. For instance, it's about the same between the two for an ArrayList,
>> but it'll be O(n) to use an Iterator over a LinkedList, and O(n*n) to use
>> get(index) for each element.
>All of them are much slower than using an array.
Hardly. For some operations, they're massively faster. Try implementing a
queue, or anything that requires inserts and deletes from the head. Let me
know when you're tired of copying your array...
>If performance is really the issue then you don't want to use any of the
>Collection objects.
Disagree. Even if perf is the issue, choosing the correct data structure for
your use will outweigh the tiny (if any) advantage of arrays over array-backed
Collections.
>But usually speed isn't the most important thing. And the collection
>classes are very easy to use.
Fully agreed. And using an iterator rather than get(int) is generally the
right way to use a collection. This way it works well regardless of
underlying structure.
--
Mark Rafn dagon@dagon.net <http://www.dagon.net/>