Wednesday, February 29, 2012

Java Nuggets


class Example implements Iterable<Integer>
{
    List<Integer> data = new LinkedList<Integer>();

    public Iterator<Integer> iterator()
    {
        return this.data.iterator();
    }
}

public class Main
{
    public static void main(String[] args)
    {
        Example ex = new Example();
        // Enhanced for loop for class that implements Iterable interface
        for (Integer i : ex)
        {
            System.out.println("Element " + i);
        }
    }
}

No comments: