Java Array Average avg(int[] xs)

Here you can find the source of avg(int[] xs)

Description

avg

License

Open Source License

Declaration

public static double avg(int[] xs) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static double avg(int[] xs) {
        if (xs.length == 0)
            return 0.0;
        else//from   w w  w.java 2 s. co m
            return ((double) sum(xs)) / xs.length;
    }

    public static double avg(double[] xs) {
        if (xs.length == 0)
            return 0.0;
        else
            return sum(xs) / xs.length;
    }

    public static int sum(int[] xs) {
        int result = 0;
        for (int x : xs)
            result += x;
        return result;
    }

    public static double sum(double[] xs) {
        double result = 0.0;
        for (double x : xs)
            result += x;
        return result;
    }
}

Related

  1. avg(final double... values)
  2. avg(float... numbers)
  3. avg(int... values)
  4. avg(int[] arr)
  5. avg(int[] values)
  6. avg(int[][] values)
  7. avgarr(int[] a)
  8. avgArray(double[] values, int maxLength)
  9. avgArrayAndDouble(double[] array, double doublevalue)