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

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

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

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?

definitely not just help him with the concepts, gotta give him credit on having a specific question

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

> 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. ;)

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.