Converts an array to an iterator of objects. - Java Collection Framework

Java examples for Collection Framework:Iterator

Description

Converts an array to an iterator of objects.

Demo Code


//package com.java2s;
import java.lang.reflect.Array;
import java.util.Iterator;

public class Main {
    /**/*from   w ww  .j a  v  a2 s. c o m*/
     * Converts an array to an iterator of objects.
     */
    public static Iterator<Object> toIteratorArray(final Object array) {
        final int length = Array.getLength(array);

        return new Iterator<Object>() {
            int index = 0;

            @Override
            public boolean hasNext() {
                return index < length;
            }

            @Override
            public Object next() {
                return Array.get(array, index++);
            }

            @Override
            public void remove() {
            }
        };
    }
}

Related Tutorials