Create a type to be used in the foreach loop. Java

foreach, java

Solution

Yes, just implement the `Iterable` interface.

From http://docs.oracle.com/javase/6/docs/api/java/lang/Iterable.html:

Implementing this interface allows an object to be the target of the "foreach" statement.

Problem

Simply is it possible to create a type, in java, which can be passed into the foreach loop after the colon? And if so, how? At first I thought I that foreach only worked for arrays, but then it came to my attention that `List`s could be iterated over. My question is then is this something that the interpreter does automatically or is it possible to create classes of my own which can be used?

Original source