We can use loops instead of using iterators. then what is need of Iterators?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

We can use loops instead of using iterators. then what is need of Iterators??

27th Jun 2016, 7:12 PM
Chiranjeevi
Chiranjeevi - avatar
2 Answers
+ 5
there are cases where for loop can't be used. For instance with a set, you can't have a direct access to elements. The only way is then to use an iterator.
27th Jun 2016, 9:16 PM
samuel delepoulle
samuel delepoulle - avatar
+ 1
Most of the time we use iterators for collection, in order to travers one object at a time. And at the same time, we can also use enhanced for loop to travers the objects in a collection. Both of these can be used based on the requirement. In order to access the elements of collection, you have to use iterators inside the loops.
28th Jun 2016, 5:05 AM
brk
brk - avatar