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 do you use compareto?

0 Answers  


What is a heavyweight component?

0 Answers  


What is a pattern what is an anti pattern?

0 Answers  


Why are global variables used?

0 Answers  


What is static keyword?

0 Answers  






Why are the methods of the Math class are static?

1 Answers  


take an array with -ve and +ve value both.find out the nearest value of 0(zero).if two values are same like(-2 and +2)then extract +2 is nearest of 0(zero).

0 Answers  


What is difference between wait and notify in java?

0 Answers  


How do constructors use this() and super()?

0 Answers  


What is the difference in between cpp and java? Can u explain in detail?

0 Answers  


What are the default and parameterized constructors?

0 Answers  


Define how objects are stored in java?

0 Answers  


Categories