Iterator
for (Iterator iter = list.iterator(); iter.hasNext();)
It seems to me that the above code could be used to print out all of the items in a list. But if the list has 1000 items, how can I change this code to print out, for example, the last 5 items on the list? I
Thank you.
> > > // assume list.size() >= 5
> >
> > Ouch if we have a large LinkedList :)
>
> Forget what I wrote here, the LinkedList rewinds if
> you chose an index close to the end :)
I was checking the same thing!
; )
Ah well, people creating really large LinkedList磗 deserve it!
> > > // assume list.size() >= 5
> >
> > Ouch if we have a large LinkedList :)
>
> Forget what I wrote here, the LinkedList rewinds if
> you chose an index close to the end :)
In my version 1.6 it tries to be clever, i.e. if index > size/2 it iterates
back from the end, otherwise it starts at the beginning. Prometheuzz'
solution for size-5 is quite cheap no matter the size of the list.
The worst case shows up at index == size/2.
kind regards,
Jos
> > > > // assume list.size() >= 5
> > >
> > > Ouch if we have a large LinkedList :)
> >
> > Forget what I wrote here, the LinkedList rewinds
> if
> > you chose an index close to the end :)
>
> In my version 1.6 it tries to be clever, i.e. if
> index > size/2 it iterates
> back from the end, otherwise it starts at the
> beginning. Prometheuzz'
> solution for size-5 is quite cheap no matter the size
> of the list.
> The worst case shows up at index == size/2.
That's why you should forget about my post :)