Is it better to use a for-each loop or an Iterator with a collection?

John Zukowski

It depends on what you need to do. The remove() method of an Iterator is the safe way to remove elements from an underlying collection. You can't safely remove elements with a for-each loop. For just "visiting" each element, either way works.

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact