ejemplo: iterable sobre un array

 

public class ArrayIterable<T>
        implements Iterable<T> {
    private final T[] objetos;

    public ArrayIterable(T[] objetos) {
        this.objetos = objetos;
    }

    public Iterator<T> iterator() {
        IteraArray<T> iterator = new IteraArray<T>(objetos);
        return iterator;
    }

}

 

 

    public static void main(String[] args) {
        String[] datos = {"a", "b", "c"};
        Iterable<String> it = new ArrayIterable<String>(datos);
        for (String s : it)
            System.out.println(s);
    }

 

Temas relacionados

21. Iterator<E> (interface) java.util.Iterator<E>