delete user

gudii9
gudii9 used Ask the Experts™
on
hi,

Wonder why screen 2 approach better than screen 1 approach.
Please advise
screen2.png
screen1.png
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Iterator mechanism (screen 2) is the slowest method to loop through a list. There's not much performance different between For and While loop. Iterator provides a very handy way to loop through a list or collection, but it is slower than For and While loop.

But in for-each loop (screen 1), we can’t modify collection, it will throw a ConcurrentModificationException on the other hand with iterator we can modify collection. An Iterator can do things that a foreach loop can't. That is better. For example, you can remove elements while you're iterating, if the iterator supports it. A foreach loop only iterates from the beginning to an end.

So,
Iterator Advantage:

    1) Ability to remove elements from Collections.
    2)  Ability to move forward and backward using next() and previous().
    3) Ability to check if there more elements or not by using hasNext().
Loop was designed only to iterate over a Collection, so if you want just to iterate over a Collection, its better to use loop such as for-Each, but if you want more that that you could use Iterator.

Commented:
There is no activity from author.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial