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.

[325 byte] By [TheMenkstera] at [2007-11-26 21:16:52]
# 1
Take a look at listIterator(int index) Kaj
kajbja at 2007-7-10 2:55:25 > top of Java-index,Java Essentials,New To Java...
# 2
// assume list.size() >= 5for(int i = list.size()-5; i < list.size(); i++) { System.out.println(list.get(i));}
prometheuzza at 2007-7-10 2:55:25 > top of Java-index,Java Essentials,New To Java...
# 3
> // assume list.size() >= 5Ouch if we have a large LinkedList :)
kajbja at 2007-7-10 2:55:25 > top of Java-index,Java Essentials,New To Java...
# 4
> > // 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 :)
kajbja at 2007-7-10 2:55:25 > top of Java-index,Java Essentials,New To Java...
# 5

> > > // 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!

prometheuzza at 2007-7-10 2:55:25 > top of Java-index,Java Essentials,New To Java...
# 6

> > > // 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

JosAHa at 2007-7-10 2:55:25 > top of Java-index,Java Essentials,New To Java...
# 7

> > > > // 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 :)

kajbja at 2007-7-10 2:55:25 > top of Java-index,Java Essentials,New To Java...