Enhanced for statement

There is an enhanced version of the for loop, also known as the “for each” loop. This type of loop iterates through elements in an array or a collection. The syntax of the enhanced for statement are the keyword for, parenthesis enclosing the type and name of the looping variable, a colon and the variable or expression referencing the array or collection, and the body of the loop is a statement or code block. The looping variable is available within the code block. The statements in the code block are repeated for each value in the array or collection:

// turn all target off
for (Target t : control.getTargets())


A Collection is an interface (a defined set of methods) for classes that supports operations on collections of data. Commonly used Collection methods include add(), remove(), size() and iterator(). The ArrayList, HashSet, LinkedList and many other classes implement the Collections interface, and the HashMap class has methods to return the keys (keyset()), values (values()) or entries (combined key and value, entrySet()) as collections.