Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.AbstractCollection;

import java.util.Collection;

import java.util.Iterator;

import java.util.NoSuchElementException;

public class Main {
    private static <E> Collection<E> asCollection(final E[] elements) {
        return new AbstractCollection<E>() {

            public Iterator<E> iterator() {
                return new Iterator<E>() {

                    // Object field
                    private int index = 0;

                    // Interface methods
                    public boolean hasNext() {
                        return index < elements.length;
                    }

                    public E next() {
                        if (!hasNext()) {
                            throw new NoSuchElementException();
                        }
                        return elements[index++];
                    }

                    public void remove() {
                        throw new UnsupportedOperationException("remove");
                    }

                };
            }

            public int size() {
                return elements.length;
            }

        };
    }
}