Java List Mean mean(List nums, int start, int size)

Here you can find the source of mean(List nums, int start, int size)

Description

Calculate the mean of a range in list of numbers.

License

Apache License

Parameter

Parameter Description
nums The numbers to get the mean of.
start The index to start taking the mean at
size The number of elements to include in the mean.

Exception

Parameter Description
IllegalArgumentException If anything is weird (null or zero list), zero size.

Return

The mean

Declaration

public static double mean(List<? extends Number> nums, int start, int size) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.List;

public class Main {
    /**/*from  w  ww. j a  va 2  s . c  o m*/
     * Calculate the mean of a range in list of numbers.
     *
     * @param nums The numbers to get the mean of.
     * @param start The index to start taking the mean at
     * @param size The number of elements to include in the mean.
     *
     * @throws IllegalArgumentException If anything is weird (null or zero list), zero size.
     *
     * @return The mean
     */
    public static double mean(List<? extends Number> nums, int start, int size) {
        if (nums == null || nums.size() == 0 || start < 0 || start >= nums.size() || size <= 0
                || start + size > nums.size()) {
            throw new IllegalArgumentException();
        }

        double mean = 0;

        for (int i = start; i < start + size; i++) {
            mean += nums.get(i).doubleValue();
        }

        return (mean / size);
    }

    /**
     * Calculate the mean of a list of numbers.
     *
     * @param nums The numbers to get the mean of.
     *
     * @throws IllegalArgumentException If anything is weird (null or zero list).
     *
     * @return The mean.
     */
    public static double mean(List<? extends Number> nums) {
        if (nums == null || nums.size() == 0) {
            throw new IllegalArgumentException();
        }

        return mean(nums, 0, nums.size());
    }
}

Related

  1. getStandardDeviation(List doubles, Double mean)
  2. getStandardDeviationOfMean(List doubles)
  3. mean(double[] list)
  4. mean(final List list)
  5. mean(List data)
  6. mean(List data)
  7. mean(List list)
  8. mean(List list)
  9. mean(List vals)