Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.Iterator;

public class Main {
    /**
     * Returns 0 if empty iterable
     * */
    public static double computeAverage(Iterable<Double> iterable) {
        synchronized (iterable) {
            Iterator<Double> iterator = iterable.iterator();
            int numberOfElements = 0;
            double sum = 0;
            while (iterator.hasNext()) {
                Double value = iterator.next();
                // FIXME the point is: why next() return null if the addition of
                // null objects is prevented in the circular fifo queue?
                // Investigate the question...
                if (value == null) {
                    iterator.remove();
                    System.out.println("null in iterator. sum: " + sum);
                } else {
                    sum += value;
                    numberOfElements++;
                }
            }
            // System.out.println("sum: " + sum + "; numner of elements: " +
            // numberOfElements);
            if (numberOfElements == 0) {
                return 0;
            }
            return sum / numberOfElements;
        }
    }
}