【问题标题】:standard deviation arraylist error标准差数组列表错误
【发布时间】:2013-04-06 01:57:07
【问题描述】:

当我在计算标准差的过程中尝试检索单个值以查找方差时出现错误。我不知道是使用 .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). 

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

【问题讨论】:

    标签: java arraylist hashmap standard-deviation


    【解决方案1】:

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

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

    我认为您想要的类似于entry.getValue().singleValues.get(i)。如果occurrences 总是等于entry.getValue().singleValues.size() 考虑去掉它。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 你能解释一下
      • @user2007843 因为我猜出现次数是您的 ArrayList 的大小(比如 6)。如果您使用i &lt;=..,那么在最后一次迭代中i 的值将是6。但是ArrayListindexed based 可动态调整大小的array,它从0 开始,以size()-1 结束,即5。因此,当您尝试在ArrayList6th 索引处获取元素时,它将抛出IndexOutOfBoundsException
      【解决方案3】:

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

      【讨论】:

      • 对不起,我应该把它放在 OP 中,但这是我尝试The operator - is undefined for the argument type(s) ArrayList&lt;Long&gt;时的错误@
      • 您需要从singleValues ArrayList 中提取一个数值来进行计算
      • 可能想要将.singleValues(i) 更改为.singleValues.get(i)
      • 好的,是的,现在我有了这个,但我刚刚得到一个 IndexOutOfBoundsException: Index 1, Size 1 temp += (entry.getValue().singleValues.get(i) - average) * ((entry.getValue().singleValues.get(i)) - average);
      猜你喜欢
      • 2014-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      相关资源
      最近更新 更多