Java Array to Iterable toIterable(final T[] array)

Here you can find the source of toIterable(final T[] array)

Description

to Iterable

License

Open Source License

Declaration

public static <T> Iterable<T> toIterable(final T[] array) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Iterator;
import java.util.NoSuchElementException;

public class Main {
    public static <T> Iterable<T> toIterable(final T[] array) {
        return new Iterable<T>() {

            @Override/*from w  w  w  .j a  v  a2  s .c  o m*/
            public Iterator<T> iterator() {
                return new Iterator<T>() {

                    private int i;

                    @Override
                    public boolean hasNext() {
                        return i < array.length;
                    }

                    @Override
                    public T next() {
                        if (!hasNext()) {
                            throw new NoSuchElementException();
                        }
                        return array[i++];
                    }

                    @Override
                    public void remove() {
                        throw new UnsupportedOperationException();
                    }
                };
            }
        };
    }
}

Related

  1. toIterable(final boolean[] values)
  2. toIterable(final T[] arr)