0

I am getting an error when I try to retrieve the individual values to find the variance in the process of calculating the standard deviation. I can't figure out whether to use .get() or .getValue and I am lost. I have already calculated the average.

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);

This is my NumberHolder class, which I populate in my main funcion. I am using this equation for standard deviation: http://www.mathsisfun.com/data/standard-deviation-formulas.html

based on my code, occurrences is N and the values from the singleValues arraylist are Xi

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

This is the error I get. :

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

If you need to see more code please just ask, I didn't want to put anything unnecessary but I might have missed something.

Vishal K
  • 12,976
  • 2
  • 27
  • 38
user2007843
  • 609
  • 1
  • 12
  • 30

3 Answers3

2

Error means exactly what it says. You can't pass an int argument to getValue().

Change entry.getValue(i) to entry.getValue() and it should work fine.

I assume it is something like entry.getValue().singleValues.get(i) that you want. If occurrences is always equal to entry.getValue().singleValues.size() consider getting rid of it.

Avi
  • 351
  • 1
  • 4
1

getValue doesn't take an integer argument. You can use:

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;
}

Also ArrayLists are zero based so you should end on size - 1.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • sorry I should have put this in the OP, but this is the error when I tried that `The operator - is undefined for the argument type(s) ArrayList` – user2007843 Apr 05 '13 at 17:39
  • You will need to extract a numeric value from the `singleValues` `ArrayList` to do a calculation though – Reimeus Apr 05 '13 at 17:42
  • 1
    Might want to change `.singleValues(i)` to `.singleValues.get(i)`. – Avi Apr 05 '13 at 17:43
  • okay yea now I have this but I just got a IndexOutOfBoundsException: Index 1, Size 1 `temp += (entry.getValue().singleValues.get(i) - average) * ((entry.getValue().singleValues.get(i)) - average);` – user2007843 Apr 05 '13 at 17:44
1

You can't take int as argument in Map.Entry#getValue(). So in your code it should be entry.getValue() instead of entry.getValue(i). Now apart from this your singleValues is an ArrayList . so you can't subtract it from integer average in line (entry.getValue(i).singleValues) - average). You have to first extract out the element from the ArrayList and then subtract it from the average. Your for loop should be something like this:

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;
}
Vishal K
  • 12,976
  • 2
  • 27
  • 38
  • Can you explain how the < rather than the <= avoids the exception? – user2007843 Apr 05 '13 at 17:57
  • @user2007843 Because I guess that occurrences is the size of your ArrayList (say 6). If you use `i <=..` then in last iteration `i` will have value `6`. But `ArrayList` is `indexed based` dynamic resizable `array` which starts from `0` and ends with `size()-1` which is `5`. So when you try to get element at `6th` index of `ArrayList` it will throws `IndexOutOfBoundsException` – Vishal K Apr 05 '13 at 18:01