What is difference between Iterator and for loop
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
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 |
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 |
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 |
Iterator is used in Collection objects,but for is used for
variables.
Is This Answer Correct ? | 15 Yes | 18 No |
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 |
Can we call thread start () twice?
Is java hashset ordered?
What are the restriction imposed on a static method or a static block of code?
What are the six ways to use this keyword?
Where pragma is used?
Is it possible to instantiate the abstract class?
Which method will get invoked first in a stand alone application?
What is classname class in java?
What is protected in java?
Is ruby built on java?
Which is the best way to use for String concatenation in Java?
What is JDBC Driver interface?How can you retrieve data from the ResultSet