Java Median Median(ArrayList values)

Here you can find the source of Median(ArrayList values)

Description

Calculates the median value from a list of numeric values.

License

Open Source License

Parameter

Parameter Description
values List of numeric values

Return

Median value

Declaration

public static double Median(ArrayList<Double> values) 

Method Source Code


//package com.java2s;
// it under the terms of the GNU General Public License as published by

import java.util.*;

public class Main {
    /** Calculates the median value from a list of numeric values.
     */*w  w  w  . j  av  a  2 s  . c o m*/
     * @param values List of numeric values
     * @return Median value
     */
    public static double Median(ArrayList<Double> values) {
        Collections.sort(values);

        if (values.size() % 2 == 1)
            return values.get((values.size() + 1) / 2 - 1);
        else {
            double lower = values.get(values.size() / 2 - 1);
            double upper = values.get(values.size() / 2);

            return (lower + upper) / 2.0;
        }
    }
}

Related

  1. getmedian(double[] vals, int nvalindex)
  2. getMedian(int[] array)
  3. getMedianValue(ArrayList values)
  4. median(ArrayList v)
  5. median(ArrayList values)
  6. median(double... a)
  7. median(double[] a)
  8. median(double[] a)
  9. median(double[] arr)