return the average value of the passed array - Android java.lang

Android examples for java.lang:array

Description

return the average value of the passed array

Demo Code

public class Main{

    /**/* w  w w. j av a 2 s.  c o  m*/
     * return the average value of the passed array.
     */
    public static double getAverage(double[] data) {
        int num = data.length;
        if (num == 0) {
            return 0;
        }

        double sum = data[0];
        for (int i = 1; i < num; i++) {
            sum += data[i];
        }
        return sum / num;
    }

}

Related Tutorials