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
Can we declare the main method of our class as private?
Why java doesn’t support multiple inheritances?
What does nextint () do in java?
What is n in java?
What is the major advantage of external iteration over internal iteration?
What is method overloading and method overriding?
What is the destroy method?
What is difference between path and classpath variables?
What is parsing and its types?
What is java console application?
What is string builder in java?
How does arrays sort work in java?
If I only change the return type, does the method become overloaded?
What are the 8 primitive data types in java?
String and stringbuffer both represent string objects. Can we compare string and stringbuffer in java?