Jose Alberto Fernandez wrote:

AFAIU, LinkedList.get(N) requires traversing the list to position N
on every call [O(n^2)], so usage of an iteratoe is much cheaper on this
case
as there is no array behind the scenes.


Jose Alberto


I've just worked out why it's ok to do it this way (with respect to my particular use-case). Basically I *want* to traverse the entire list, I'm not trying to pick out any particular position in the list (yes truly expensive using LinkedList.get(i)). In my method I simply call get(i) from 0..List.size():

from docs
<quote>Operations that index into the list will traverse the list from the begining or the end, whichever is closer to the specified index.</quote>


So as I'm asking for position 0, the traversal starts at the head of the list, then I simply walk the list with the loop calling get(i), which also happens to be the next element in the list from where the list cursor currently is (ListIterator interface docs). Serendipity I suppose, but my usage is the most efficient for LinkedLists and also happens to be performant with ArrayLists, to do the particular traversal that I'm doing.

Kev

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to