有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java标准差数组列表错误

在计算标准偏差的过程中,当我试图检索单个值以找到方差时,我遇到了一个错误。我不知道该不该用。获取()或。获得价值,我就迷路了。我已经计算了平均数

final ArrayList<Map.Entry<String,NumberHolder>> entries = new ArrayList<Map.Entry<String,NumberHolder>>(uaCount.entrySet());


for(Map.Entry<String,NumberHolder> entry : entries)  //iterating over the sorted hashmap
{

    double temp = 0;
    double variance = 0;

    for (int i = 0; i <= entry.getValue().occurrences ; i ++)
        {               
            temp += ((entry.getValue(i).singleValues) - average)*((entry.getValue(i).singleValues) - average);

            variance = temp/entry.getValue().occurrences;
        }

        double stdDev = Math.sqrt(variance);

这是我的NumberHolder类,我在主函数中填充了它。我用这个方程来表示标准偏差:http://www.mathsisfun.com/data/standard-deviation-formulas.html

根据我的代码,出现次数为N,singleValues arraylist中的值为Xi

public static class NumberHolder
{
    public int occurrences = 0;
    public int sumtime_in_milliseconds = 0; 
    public ArrayList<Long> singleValues = new ArrayList<Long>();
}

这就是我得到的错误

The method getValue() in the type Map.Entry<String,series3.NumberHolder> is not applicable for the arguments (int). 

如果你需要看更多的代码,请直接问,我不想放任何不必要的东西,但我可能错过了一些东西


共 (3) 个答案

  1. # 1 楼答案

    错误就是它所说的。不能将int参数传递给getValue()

    entry.getValue(i)更改为entry.getValue(),它应该可以正常工作

    我猜你想要的是entry.getValue().singleValues.get(i)。如果^ {CD5> }总是等于^ {< CD6> },请考虑删除它。

  2. # 2 楼答案

    getValue不接受整数参数。你可以使用:

    for (int i = 0; i < entry.getValue().singleValues.size(); i++) {
       Long singleValue = entry.getValue().singleValues.get(i);
       temp += (singleValue - average) * (singleValue - average);
    
       variance = temp / entry.getValue().occurrences;
    }
    

    而且ArrayLists是以零为基础的,所以应该以size - 1结束

  3. # 3 楼答案

    不能将int作为^{}中的参数。所以在你的代码中,它应该是entry.getValue()而不是entry.getValue(i)。除此之外,你的singleValues是一个ArrayList。所以你不能从第(entry.getValue(i).singleValues) - average)行的整数average中减去它。必须首先从ArrayList中提取元素,然后从average中减去它。for循环应该是这样的:

    for (int i = 0; i < entry.getValue().occurrences ; i ++)// i < entry.getValue() to avoid IndexOutOfBoundsException
    {               
       temp += ((entry.getValue().singleValues.get(i)) - average)*((entry.getValue().singleValues.get(i)) - average);
       variance = temp/entry.getValue().occurrences;
    }