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
What is array command?
Is char * a string?
What is a function in java?
What super () does in java?
Convert a BST into a DLL and DLL to BST in place.
What is toarray method in java?
What is backdrop?
Where we write javascript code in html page?
What is multithreading in java?
What is a loop java?
What state does a thread enter when it terminates its processing in java programming?
What is a function in programming?
What do you mean by mnemonics?
What is anti pattern in cyber security?
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).