What is difference between Iterator and for loop

Answer Posted / deepesh

An "Iterator object" is used to move through a Collection,
by calling Collection's iterator() method. By using for
loop, you are limited to that particular collection you are
traversing, while iterator gives freedom to work with any
collection.

iterator's job is to move through the sequence (without the
client programmer knowing or caring about the underlying
structure & its modification) using hasNext() and next().
remove() is provided "to remove the last element returned by
the iterator" and is called only per call after next().

Iterator can be used for Lists and Sets for forward
(unidirectional) traversal of elements without knowing their
size, which is nearly same concept for for-each loop.

Is This Answer Correct ?    6 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can we create object of static class?

526


What is a protected class in java?

535


Why java is considered dynamic?

750


What does s mean in regex?

549


Is integer immutable in java?

547






Explain the private protected method modifier?

542


what is bmg file and how to create that files?what will it contailn?

1884


What is a map in java?

548


Is java a compiler?

565


Is there any tag in htm to upload and download files?

604


Is java based on c?

532


What is difference between printf and scanf?

516


What is regex java?

556


Can inner class final?

631


What kind of variables can a class consist?

581