Java Array Average avg(int[] arr)

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

Description

AVG all elements of the specified primitive type array.

License

Open Source License

Parameter

Parameter Description
arr the array to be AVG

Declaration

public static float avg(int[] arr) 

Method Source Code

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

public class Main {

    public static float avg(int[] arr) {
        return (float) sum(arr) / arr.length;
    }//from  ww  w .  jav  a 2s .c o  m

    public static float avg(double[] arr) {
        return (float) sum(arr) / arr.length;
    }

    public static float avg(float[] arr) {
        return (float) sum(arr) / arr.length;
    }

    public static float avg(long[] arr) {
        return (float) sum(arr) / arr.length;
    }

    public static float avg(short[] arr) {
        return (float) sum(arr) / arr.length;
    }

    public static float avg(byte[] arr) {
        return (float) sum(arr) / arr.length;
    }

    public static int sum(int[] arr) {
        int sum = 0;
        for (int i = 0, l = arr.length; i < l; i++) {
            sum += arr[i];
        }
        return sum;
    }

    public static double sum(double[] arr) {
        double sum = 0;
        for (int i = 0, l = arr.length; i < l; i++) {
            sum += arr[i];
        }
        return sum;
    }

    public static float sum(float[] arr) {
        float sum = 0;
        for (int i = 0, l = arr.length; i < l; i++) {
            sum += arr[i];
        }
        return sum;
    }

    public static long sum(long[] arr) {
        long sum = 0;
        for (int i = 0, l = arr.length; i < l; i++) {
            sum += arr[i];
        }
        return sum;
    }

    public static short sum(short[] arr) {
        short sum = 0;
        for (int i = 0, l = arr.length; i < l; i++) {
            sum += arr[i];
        }
        return sum;
    }

    public static byte sum(byte[] arr) {
        byte sum = 0;
        for (int i = 0, l = arr.length; i < l; i++) {
            sum += arr[i];
        }
        return sum;
    }
}

Related

  1. avg(double[] x, int start, int end)
  2. avg(final double a, final double b)
  3. avg(final double... values)
  4. avg(float... numbers)
  5. avg(int... values)
  6. avg(int[] values)
  7. avg(int[] xs)
  8. avg(int[][] values)
  9. avgarr(int[] a)