Java mean mean(int[] array)

Here you can find the source of mean(int[] array)

Description

Computes the mean value of an array of integers.

License

Open Source License

Parameter

Parameter Description
array Array of integers whose mean is to be computed.

Return

Mean value of the array.

Declaration

public static double mean(int[] array) 

Method Source Code

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

public class Main {
    /**/*from ww  w  . j  av a2s. c o m*/
     * Computes the mean value of a matrix of integers.
     *
     * @param matrix   A matrix of integers whose mean is to be computed.
     * @return   Mean value of the matrix.
     */
    public static double mean(int[][] matrix) {

        int width = matrix.length;
        int height = matrix[0].length;
        double sum = 0;

        for (int j = 0; j < width; j++) {
            for (int i = 0; i < height; i++) {
                sum += matrix[j][i];
            }
        }
        double mean = sum / (width * height);

        return mean;
    }

    /**
     * Computes the mean value of a matrix of doubles.
     *
     * @param matrix   Matrix of doubles whose mean is to be computed.
     * @return   Mean value of the matrix.
     */
    public static double mean(double[][] matrix) {

        int width = matrix.length;
        int height = matrix[0].length;
        double sum = 0;

        for (int j = 0; j < width; j++) {
            for (int i = 0; i < height; i++) {
                sum += matrix[j][i];
            }
        }
        double mean = sum / (width * height);

        return mean;
    }

    /**
     * Computes the mean value of a matrix of longs.
     *
     * @param matrix   Matrix of longs whose mean is to be computed.
     * @return   Mean value of the matrix.
     */
    public static double mean(long[][] matrix) {

        int width = matrix.length;
        int height = matrix[0].length;
        double sum = 0;

        for (int j = 0; j < width; j++) {
            for (int i = 0; i < height; i++) {
                sum += matrix[j][i];
            }
        }
        double mean = sum / (width * height);

        return mean;
    }

    /**
     * Computes the mean value of a matrix of floats.
     *
     * @param matrix   Matrix of floats whose mean is to be computed.
     * @return   Mean value of the matrix.
     */
    public static float mean(float[][] matrix) {

        int width = matrix.length;
        int height = matrix[0].length;
        double sum = 0;

        for (int j = 0; j < width; j++) {
            for (int i = 0; i < height; i++) {
                sum += matrix[j][i];
            }
        }
        float mean = (float) (sum / (width * height));

        return mean;
    }

    /**
     * Computes the mean value of a matrix of bytes.
     *
     * @param matrix   Matrix of bytes whose mean is to be computed.
     * @return   Mean value of the matrix.
     */
    public static float mean(byte[][] matrix) {

        int width = matrix.length;
        int height = matrix[0].length;
        double sum = 0;

        for (int j = 0; j < width; j++) {
            for (int i = 0; i < height; i++) {
                sum += matrix[j][i];
            }
        }
        float mean = (float) (sum / (width * height));

        return mean;
    }

    /**
     * Computes the mean value of an array of integers.
     *
     * @param array   Array of integers whose mean is to be computed.
     * @return   Mean value of the array.
     */
    public static double mean(int[] array) {

        double sum = 0;

        for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }
        double mean = sum / array.length;

        return mean;
    }

    /**
     * Computes the mean value of an array of doubles.
     *
     * @param array   Array of doubles whose mean is to be computed.
     * @return   Mean value of the array.
     */
    public static double mean(double[] array) {

        double sum = 0;

        for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }
        double mean = sum / array.length;

        return mean;
    }

    /**
     * Computes the mean value of an array of longs.
     *
     * @param array   Array of longs whose mean is to be computed.
     * @return   Mean value of the array.
     */
    public static double mean(long[] array) {

        double sum = 0;

        for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }
        double mean = sum / array.length;

        return mean;
    }

    /**
     * Computes the mean value of an array of floats.
     *
     * @param array   Array of floats whose mean is to be computed.
     * @return   Mean value of the array.
     */
    public static double mean(float[] array) {

        double sum = 0;

        for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }
        double mean = sum / array.length;

        return mean;
    }

    /**
     * Computes the mean value of an array of bytes.
     *
     * @param array   Array of bytes whose mean is to be computed.
     * @return   Mean value of the array.
     */
    public static double mean(byte[] array) {

        double sum = 0;

        for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }
        double mean = sum / array.length;

        return mean;
    }
}

Related

  1. mean(float[] array)
  2. mean(float[] xs)
  3. mean(int low, int high)
  4. mean(int N, int D, double dat[][], double mu[])
  5. mean(int... values)
  6. mean(int[] input)
  7. mean(int[] values)
  8. mean(int[] values, int max)
  9. mean(Integer[] values)