import java.util.*;
import java.io.PrintWriter;

        /* An example of creating an inner class Iterator.
         * Collect an arbitrary number of double values.
         * Provide access to individual items and the mean value,
         * for instance.
         * Allow selection of all positive values in the collection.
         */
public class DataValues {
        // Add a value to the collection.
    public void add(double d){
        getNumbers().add(new Double(d));
    }

        // Get a value from the collection.
    public double get(int index) throws IndexOutOfBoundsException {
        Double d = (Double) getNumbers().get(index);
        return d.doubleValue();
    }

        // Calculate the mean of the values in the collection.
    public double mean(){
        final int howMany = numItems();
        if(howMany > 0){
            double sum = 0.0;
            for(int i = 0; i < howMany; i++){
                sum += get(i);
            }
            return sum / howMany;
        }
        else{
            return 0.0;
        }
    }

        // Print the collection.
    public void print(PrintWriter writer){
        final int howMany = numItems();
        for(int i = 0; i < howMany; i++){
            writer.print(get(i)+" ");
        }
        writer.println();
        writer.flush();
    }

        // Return how many items in the collection.
    public int numItems(){
        return getNumbers().size();
    }
    
        // Obtain an iterator over the positive values in the collection.
    public Iterator getPositives(){
        return new PositiveSelector();
    }

        // An inner class to allow identification of the positive
        // values (> 0) in the collection.
    protected class PositiveSelector implements Iterator {
        public boolean hasNext(){
            return nextPosition() >= 0;
        }

            // Return the next positive data item, if there is one.
        public Object next() throws NoSuchElementException {
            // Find out where the next (if any) occurs.
            int i = nextPosition();
            if(i >= 0){
                Object o = getNumbers().get(i);
                // Next time, start looking from the following position.
                setIndex(i+1);
                return o;
            }
            else{
                throw new NoSuchElementException();
            }
        }

            // Disallow removal.
        public void remove() throws UnsupportedOperationException {
            throw new UnsupportedOperationException();
        }

            // Return the index of the next positive number in the collection.
            // If there is none, return NOITEM.
        protected int nextPosition(){
            int i = getIndex();
            final int howMany = numItems();
            while(i < howMany){
                if(get(i) > 0){
                    return i;
                }
                i++;
            }
            return NOITEM;
        }

        protected int getIndex(){
            return index;
        }

        protected void setIndex(int i){
            index = i;
        }

        // The index of the next item to be examined.
        private int index = 0;
        // An illegal index to indicate no item is available.
        private final int NOITEM = -1;
    }
    
    protected LinkedList getNumbers(){
        return numbers;
    }
    
    // A collection of Double objects.
    private final LinkedList numbers = new LinkedList();
}
