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 a constructor call another constructor?
What is JVM and is it platform independent?
What flag up means?
What is your platform?s default character encoding and how to know this?
If an application has multiple classes in it, is it okay to have a main method in more than one class?
What do you mean by global variable?
can used Protected Class outside Function.?
Tell me the Importent classes in net package?
What is function declaration?
What is user defined exception in Java?
What are the pillars of java?
How do you read a char in java?
What are the differences between string, stringbuffer and stringbuilder?
Does java initialize arrays to zero?
When a lot of changes are required in data, which one should be a preference to be used? String or stringbuffer?