【问题标题】:How do you invoke a method with the signature "public double getMedian(double[]list) {//code}"如何调用带有签名“public double getMedian(double[]list) {//code}”的方法
【发布时间】:2014-10-09 21:20:31
【问题描述】:

我想调用此方法从数组中获取中位数。该方法被声明为public double getMedian(double[]list){//code}

我尝试将该方法称为getMedian(double,list),但出现错误。调用该方法的正确方法是什么?

这里是完整的方法:

public double getMedian(double[] list) {
    // calculate the length of the entries
    // create an iterator
    int factor = list.length - 1;
    double[] first = new double[(int) ((double) factor / 2)];
    double[] last = new double[first.length];
    double[] middleNumbers = new double[1];

    for (int i = 0; i < first.length; i++) {
        first[i] = list[i];
    }

    for (int i = list.length; i > last.length; i--) {
        last[i] = list[i];
    }

    for (int i = 0; i <= list.length; i++) {
        if (list[i] != first[i] || list[i] != last[i])
            middleNumbers[i] = list[i];
    }

    if (list.length % 2 == 0) {
        double total = middleNumbers[0] + middleNumbers[1];
        return total / 2;
    } else {
        System.out.println(middleNumbers);
        return middleNumbers[0];
    }
}

【问题讨论】:

  • 它要求一个双精度数组。 “list”只是它用于该数组的名称。

标签: java methods call


【解决方案1】:

该方法将一个双精度值数组作为参数。你会想要创建一个 double 数组并传递它:

double[] values = {0.1d, 0.3d, 0.5d, 1.0d, 1200.0d};        
double median = this.getMedian(values); // should return 0.5d

getMedian 方法存在一些逻辑错误,使其无法正常工作。特别是,第二个循环开始于数组边界之外:

 for (int i = list.length; i > last.length; i--) {
     last[i] = list[i];
 }

使用我的测试数据,i5 开始倒计时,直到i 大于5,但该数组只有索引从04 的元素。

【讨论】:

  • 我明白了,谢谢您的帮助!我会将其标记为已回答,因为它引导我朝着正确的方向前进。谢谢!
【解决方案2】:

它只要求一个双精度列表,即您刚刚为数组命名的名称。你需要这样做

getMedian(double[] doubleArray, List list)

在调用时使用 2 种不同的类型。

【讨论】:

    【解决方案3】:

    像这样(可以说包括更优雅、更有效的 getMedian 版本):

    public class Test 
    {
        public double getMedian(double[] list)
        {
            double median = 0;
    
            if (list != null && (list.length > 0)) 
            {
                // Sort ascending
                Arrays.sort(list);
    
                int numItems = list.length;
    
                if ((numItems % 2) == 0)
                {
                    // We have an even number of items - average the middle two
                    int middleIndex = numItems / 2;
                    double firstMiddleValue = list[middleIndex - 1];
                    double secondMiddleValue = list[middleIndex];
                    median = (firstMiddleValue + secondMiddleValue) / 2;
                } 
                else
                {
                    // Odd number of items - pick the middle one
                    median = list[(numItems / 2)];
                }
            }
    
            return median;
        }
    
        public static void main(String[] args)
        {
            Test t = new Test();
    
            double[] testValuesOfLengthOne = { 3.1415 };
            double[] testValuesEvenLength = {22.5, 14.33, 100.849, 44.259, 0.0, 145000.0};
            double[] testValuesOddLength = {22.5, 14.33, 100.849, 44.259, 0.0,  145000.0, -4.9};
    
            System.out.println("Median of " + Arrays.toString(testValuesOfLengthOne)    + " is " + t.getMedian(testValuesOfLengthOne));
            System.out.println("Median of " + Arrays.toString(testValuesEvenLength) + " is " + t.getMedian(testValuesEvenLength));
            System.out.println("Median of " + Arrays.toString(testValuesOddLength) + " is " + t.getMedian(testValuesOddLength));
        }
    }
    

    这将为您提供以下输出:

    Median of [3.1415] is 3.1415
    Median of [22.5, 14.33, 100.849, 44.259, 0.0, 145000.0] is 33.3795
    Median of [22.5, 14.33, 100.849, 44.259, 0.0, 145000.0, -4.9] is 22.5
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-21
      相关资源
      最近更新 更多