What are the advantages of using iterator in linkedlists or arraylists? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What are the advantages of using iterator in linkedlists or arraylists?

25th Aug 2017, 11:45 AM
Mohamad Sharifi
Mohamad Sharifi - avatar
2 Answers
+ 10
You don't have to deal with iterators anymore... you can use forEach() on the collections or the enhanced for loop and the API handles the iterators internally then.
25th Aug 2017, 11:52 AM
Tashi N
Tashi N - avatar
- 1
But iterators come in handy if you need to modify collection in your loop. For example if you want to remove an element while looping with iterators you can, but with foreach you get an exception.
26th Aug 2017, 10:37 PM
Mohamad Sharifi
Mohamad Sharifi - avatar