找到数组的中位数并返回true

时间:2015-02-11 06:33:32

标签: java arrays

如果数组中的数字是数组的中位数,我必须编写一个返回true的方法。如果大于m的元素的数量与小于m的元素的数量相同,则中值m是中值。老实说,我完全陷入困境,不知道如何启动它...我只想获得提示。谢谢!

public boolean isMedian(double[] sample, double m) {
    //just what i have so far
    boolean median;
    for(int i = 0; i < sample.length; i++) {

    }
}

1 个答案:

答案 0 :(得分:2)

试试这个:

public boolean isMedian(double[] sample, double m) {
    if ((sample.length % 2) == 0) return false;
    double[] lSample = new double[sample.length];
    System.arraycopy( sample, 0, lSample, 0, sample.length );
    Arrays.sort(lSample);
    double median = lSample[lSample.length/2]
    return median==m;
}