2

I am trying to calculate Standerd Deviation,mean avarage etc. I am trying to write below code. but it thows exception java.util.ConcurrentModificationException.I am tryng to fix it but faild.

My code:

package statistic;

import java.util.*;

public class StanderDeviation {

    public static void main(String[] args) {

        System.out.println("step 1:create a list of 10 numbers :");

        List<Double> numbers = new ArrayList<Double>();
        numbers.add(23.0);
        numbers.add(92.0);
        numbers.add(46.0);
        numbers.add(55.0);
        numbers.add(63.0);
        numbers.add(94.0);
        numbers.add(77.0);
        numbers.add(38.0);
        numbers.add(84.0);
        numbers.add(26.0);
        System.out.println("\n " +numbers);

        System.out.println("step 2: average  of 10 numbers : ");

        double sum = 0;
        for(double number : numbers)
        {
            sum= sum +number;
        }
        double average = sum / numbers.size();
        System.out.println(sum + " / " + numbers.size() + " = " + average);

        System.out.println("\nstep 3: difference between each number numbers and average :");

        List<Double>listOfDifferences = new ArrayList<Double>();
        for(double number : numbers)
        {
            double difference = number - average ;
            listOfDifferences.add(difference);
            System.out.printf("%2.2f", difference);
            System.out.printf(" "); 
        }


         System.out.println("\n\nstep 4: Square of above calculate difference :");

        List<Double> squares = new ArrayList<Double>();
        for(double difference : listOfDifferences)
        {
            double square = difference * difference ;
            listOfDifferences.add(difference);
            squares.add(square);
            System.out.printf("%4.2f", square);
            System.out.printf(" "); 
        }
        System.out.println("\nstep 5:sum of Square   calculate above :");

        sum = 0;
        for(double number : squares)
        {
            sum += number ; 
        }
        System.out.println(sum);
        System.out.println();

        System.out.println("step 6: divide sum of Square by totle (numbers)-1 :");

        double result = sum / (numbers.size()-1);
        System.out.printf("%4.2f", result);
        System.out.println();

        System.out.println("\nstep 7: Stander Deviation is the Square root of obove result :");

        double StanderDeviation = Math.sqrt(result);
        System.out.printf("%4.2f", StanderDeviation);
    }
}

And here is the exeption error

Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
    at java.util.ArrayList$Itr.next(Unknown Source)
    at statistic.StanderDeviation.main(StanderDeviation.java:50)

Thanks in advance.

ParkerHalo
  • 4,341
  • 9
  • 29
  • 51
Faisal
  • 4,591
  • 3
  • 40
  • 49
  • Don't modify the lists you are iterating over by adding or deleting items in them. – TT. Feb 26 '16 at 08:19
  • sorry i am not understand. i am new in java. can you explain it. – Faisal Feb 26 '16 at 08:21
  • Hope [this](http://stackoverflow.com/a/8189786/3288182) helps – Scar Feb 26 '16 at 08:21
  • In a for loop like `for(double difference : listOfDifferences){...}` you cannot add or delete items in the `listOfDifferences` list. – TT. Feb 26 '16 at 08:22

2 Answers2

1

Your problem lies within this loop:

for(double difference : listOfDifferences)
{
    double square = difference * difference ;
    listOfDifferences.add(difference);
    squares.add(square);
    System.out.printf("%4.2f", square);
    System.out.printf(" "); 
}

You are trying to add something to the list you're currently iterating over!

Just remove the line listOfDifferences.add(difference);!

It will not affect your calculation of standard deviation. (And it doesn't really make sense to add the difference you just received from the list listOfDifferences back to the same list again!)

ParkerHalo
  • 4,341
  • 9
  • 29
  • 51
1

You could create other List structure(if it's really needed in your case) and add elements to it like:

   List<Double> changed = new ArrayList<Double>();

        for(double difference : listOfDifferences)
        {
            double square = difference * difference ;
//            listOfDifferences.add(difference);
            squares.add(square);
            changed.add(difference);
            System.out.printf("%4.2f", square);
            System.out.printf(" ");
        }

It's not possible modify Collection which you iterate on.

ConcurrentModificationException Oracle doc.

sergionni
  • 13,290
  • 42
  • 132
  • 189