Java tutorial
//package com.java2s; import java.util.Iterator; public class Main { /** * Convience method for wrapping an iterator into an iterable. * This is used mainly for using of a foreach loop with an iterator * * @param it * @return */ public static <T> Iterable<T> getIterable(final Iterator<T> it) { return new Iterable<T>() { public Iterator<T> iterator() { return it; } }; } /** * Generate an instance of Iterable for an array. * This may be used in a foreach loop. * * @param array * @return */ public static <T> Iterable<T> getIterable(final T[] array) { return new Iterable<T>() { public Iterator<T> iterator() { return getIterator(array); } }; } /** * Generate an iterator for an array * * @param array * @return */ public static <T> Iterator<T> getIterator(final T[] array) { return new Iterator<T>() { private int i = 0; public boolean hasNext() { return i < array.length; } public T next() { return array[i++]; } public void remove() { throw new UnsupportedOperationException(); } }; } }