Question: I think most coders have used code like the following :

```

ArrayList<String> myStringList = getStringList();
for(String str : myStringList)
{
   doSomethingWith(str);
}

```

How can I take advantage of the for each loop with my own classes? Is there an interface I should be implementing?

Answer: 
You can implement [Iterable](http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Iterable.html).

Here's an [example](http://www.java2s.com/Tutorial/Java/0140__Collections/CreatingIterableObjectsusingaforeachforlooponanIterableobject.htm). It's not the best, as the object is its own iterator. However it should give you an idea as to what's going on.
You have to implement the Iterable [interface](http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Iterable.html), that is to say, you have to implement the method

```
class MyClass implements Iterable<YourType>
{
Iterator<YourType> iterator()
  {
  return ...;//an iterator over your data
  }
}

```