What is difference between Iterator and for loop

Answers were Sorted based on User's Feedback



What is difference between Iterator and for loop..

Answer / bapi paul

An Iterator is an Object, which goes through a Collection
and you can do something with whatever the Iterator is
pointing at.
One big advantage is, that you don't have to know the size
of your Collection.


A Loop is a construct, that repeats something for a certain
amount of times.
One big advantage is, that you always know, at which
position you are.

Is This Answer Correct ?    62 Yes 5 No

What is difference between Iterator and for loop..

Answer / vijayakumar chinnasamy

In iterator the remove() is available to remove the
content/item but in for loop remove() not available.

Is This Answer Correct ?    52 Yes 10 No

What is difference between Iterator and for loop..

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

What is difference between Iterator and for loop..

Answer / punamchand

Iterator has its own method remove() which is used to remove data while for each don't have its own method

Is This Answer Correct ?    5 Yes 2 No

What is difference between Iterator and for loop..

Answer / amit r

There are basic 3 differences between For and Iterater.
1.
Using Iterater we can check if the object exists or not by using hasNext method. Where as in For Loop, there is not such method. Therefore, For Loop will be executed at fixed amount at every time.
2.
Using Iterator we can add or remove objects from the underlying collection. In For Loop, if we do so, concurrent modification exception will be thrown.
3.
Using Iterator we can move forward or backward where as For Loop can't.
:)

Is This Answer Correct ?    7 Yes 6 No

What is difference between Iterator and for loop..

Answer / a.jyotsna

The Iterator is not implemented as an
indexed loop, at least not in Sun Java. The collection
always has a
getter armed with the iterator's next value. Much of the
collection's
internal implementation is done via the iterator. But the
pieces of
ArrayList that are optimized for speed, skip bounds
checking and use int
array offsets to do their work.
I would like to know what is the difference between these 2.
>
> ArrayList list = new ArrayList
>
> for(int i=0; i<list.size(); i++){
> //do stuff
> }
>
> Iterator i = list.iterator();
>
> while(i.hasNext(){
> do stuff
> }

Is This Answer Correct ?    6 Yes 6 No

What is difference between Iterator and for loop..

Answer / tulasi ram damarla

Also, iterator is can be used with set to iterate and to
read an element from set. using for loop it is possible to
iterate,but you cannot read an element, because set does
not have a get() method.

Is This Answer Correct ?    5 Yes 6 No

What is difference between Iterator and for loop..

Answer / srikanth nallapaneni

Iterator is used in Collection objects,but for is used for
variables.

Is This Answer Correct ?    15 Yes 18 No

What is difference between Iterator and for loop..

Answer / sundarakannan d

The major difference is Iterator doesn't output the list we
are iterating in sequence.
where as in for we structure it to print in sequence.

Is This Answer Correct ?    6 Yes 21 No

Post New Answer

More Core Java Interview Questions

Is java type safe?

0 Answers  


What is the need of transient variables in Java ?

0 Answers   MCN Solutions,


In real time project which driver did u use? What is the main functionality of the Prepared Statement?

3 Answers   Photon,


Define class?

0 Answers  


What is the purpose of the System class?

0 Answers  






What are event-delegation model and event-inheritance model? Which is best?

1 Answers  


What is meant by inheritance and what are its advantages?

0 Answers  


What is double parsedouble in java?

0 Answers  


List some java keywords sun like c, c + + keywords?

0 Answers  


How can you share data between two thread in Java?

0 Answers  


Can arraylist contain null values?

0 Answers  


How do I convert a numeric ip address like 192.18.97.39 into a hostname like java.sun.com?

0 Answers  


Categories