繁体   English   中英

标准差arraylist错误

[英]standard deviation arraylist error

尝试检索各个值以在计算标准偏差的过程中找到方差时出现错误。 我不知道该使用.get()还是.getValue,我迷路了。 我已经计算了平均值。

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数组列表中的值为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). 

如果您需要查看更多代码,请问,我不想放任何不必要的东西,但我可能会错过一些东西。

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

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

我假设它是您想要的像entry.getValue().singleValues.get(i)东西。 如果occurrences始终等于entry.getValue().singleValues.size()考虑摆脱它。

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结尾。

您不能在Map.Entry#getValue()中将int作为参数。 因此,在您的代码中,它应该是entry.getValue() 而不是 entry.getValue(i) 现在除此之外,您的singleValuesArrayList 因此您不能从行中的整数average (entry.getValue(i).singleValues) - 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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM