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).
如果你需要看更多的代码,请直接问,我不想放任何不必要的东西,但我可能错过了一些东西
# 1 楼答案
错误就是它所说的。不能将int参数传递给
getValue()
将
entry.getValue(i)
更改为entry.getValue()
,它应该可以正常工作我猜你想要的是
entry.getValue().singleValues.get(i)
。如果^ {CD5> }总是等于^ {< CD6> },请考虑删除它。# 2 楼答案
getValue不接受整数参数。你可以使用:
而且
ArrayLists
是以零为基础的,所以应该以size - 1
结束# 3 楼答案
不能将} 中的参数。所以在你的代码中,它应该是
int
作为^{entry.getValue()
而不是entry.getValue(i)
。除此之外,你的singleValues
是一个ArrayList
。所以你不能从第(entry.getValue(i).singleValues) - average)
行的整数average
中减去它。必须首先从ArrayList
中提取元素,然后从average
中减去它。for循环应该是这样的: