类型不兼容

盟国

我正在尝试创建一种返回数组模式的方法(我不是在问如何制作方法,而是希望您不要给我任何想法。对于那些一直批评我的问题是别人重复的人,我知道答案已经在这个网站上了。我在名为BasicMathMethods的类中创建了一个名为getTotal的方法:

public static int getTotal(double[] a, int b)
{
    int count = 0;
    int Element;
    for(Element = 0; Element < a.length; Element++)
    {
        if(a[b] == a[Element])
        {
            count++;
        }
    }
    return count;
}

我尝试在我的mode方法中使用此方法:

 public static double Mode(double[] a)
{
    for(int element = 0; element < a.length; element++)
    {
        int[] largest = BasicMathMethods.getTotal(a, element);// gets the count of each element
        Arrays.sort(largest);
        if(BasicMathMethods.getTotal(a, element) == largest.length)
        return a[element];
    }
}

但是,我的编译器(顺便说一下是Blue J)说“不兼容的类型:int无法转换为int []”,并突出显示(a,元素)。我不明白这个错误。我看不到要在哪里将int []转换为int的地方。谁能告诉我错误是什么并解释吗?

德博斯密特·雷

如果要将从BasicMathMethods.getTotal(a, element)数组中获得的值添加到数组中,则会遇到一些麻烦。您不知道会得到多少个值。因此,您将无法知道所需的阵列大小。

使用ArrayList而不是数组。

但更重要的是。我想指出您在获取证书时所遇到的主要问题mode阅读下面的代码,看是否有意义。

public static double mode(double[] a) {

    double currentBestCandidate = a[0];
    int countCurrentBestCandidate = BasicMathMethods.getTotal(a, 0);

    // we already have index 0, lets start from 1
    for(int element = 1; element < a.length; element++) {
        int count = BasicMathMethods.getTotal(a, element);

        // if this count is greater than what we have 
        // right now, we need to update currentBestCandidate 
        // and countCurrentBestCandidate
        if(count > countCurrentBestCandidate) {
            currentBestCandidate = a[element];
            countCurrentBestCandidate = count;
        }
    }
    return currentBestCandidate;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章