Today, I was happily coded when I received code that I have already used hundreds of times:
Iterate through a collection (here is an ArrayList)
for some reason, I really looked at the Eclipse autocomplete options, and I wondered:
In what cases is it better to use the following loops than others?
Classic array index loop:
for (int i = 0; i < collection.length; i++) { type array_element = collection.get(index); }
HasNext iterator () / next ():
for (Iterator iterator = collection.iterator(); iterator.hasNext();) { type type = (type) iterator.next(); }
And my love, because itβs so easy to write it:
for (iterable_type iterable_element : collection) { }
java collections for-loop
Jason Rogers Mar 08 2018-11-11T00: 00Z
source share