0

hi guys
i need help with list collection in java
can somebody give me some good example of how to iterate over list, also explain little bit. Thank you

6
Contributors
8
Replies
9
Views
9 Years
Discussion Span
Last Post by ~s.o.s~
0

every kind of iteration can be used: for, while, do, ...

just try and follow the next pseudo-code

determine numberOfElementsInList

do (numberOfElementsInList) times
processElement

that'ssss... all there's to it, actually.
just program how you think this should be used in Java using a List, and if there are any errors, just show us your code

0

you don't have to do it this way, this is typically my guideline

i normally would use a for loop with a known size and a while loop where you don't know the size until the last iteration

0

you don't have to do it this way, this is typically my guideline

i normally would use a for loop with a known size and a while loop where you don't know the size until the last iteration

which is the same as I posted :)
I was writing in a simplistic form of pseudo-code, not in Java. the easiest way for him to go is a for, since he has a list and can easily determin the number of elements in it, but we can't do it all for him, now can we?

0

> can somebody give me some good example of how to iterate over list,

Just use an Iterator / ListIterator for iterating over a List in a implementation agnostic manner. Iterators have the benefit of being able to allow you to add / remove an element without any additional effort.

Using a simple looping construct for a LinkedList has serious performance implications since each get() walks the list till the element with the given index is found i.e. it's not a O(1) operation as in the case of an ArrayList but a O(n) operation.

0

> can somebody give me some good example of how to iterate over list,

...

Using a simple looping construct for a LinkedList has serious performance implications ...

Except for the "new" for loop

for (<Type> var : listVar)

which implements an Iterator on it's own. ;)

0

Oh yes, the enhanced for loop certainly deserves its own mention. But I guess Iterators still have an upper hand here given the feature that one can add/remove elements when iterating. :-)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.