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

How would you format a date in java? I.e. In the ddmmyyy format?

0 Answers  


How transient variable is different from volatile variable?

0 Answers  


Can try statements be nested?

3 Answers   Wipro,


what are the boundaries of jdk and jre?

0 Answers   CoreObjects,


Class c implements interface I containing method m1 and m2 declarations. Class c has provided implementation for method m2. Can I create an object of class c?

0 Answers  






What is a function in programming?

0 Answers  


Does java runtime require a license?

0 Answers  


What do you understand by Header linked List?

0 Answers   Genpact,


what is run time polymorphism

4 Answers  


Why string is immutable with example?

0 Answers  


Why main function is static?

0 Answers   MCN Solutions,


Is a method a procedure?

0 Answers  


Categories