ArrayList Addition
in
Programming Questions
•
2 years ago
So I'm trying to use arraylist instead of a regular array so that it can be dynamically sized. Part of the code includes finding standard deviation and average in order to get rid of outliers. It worked great when I was using regular arrays but not that it is arraylist its not happy. The problem seems to be that the get() function returns objects which you cant add to ints. Problem is that I dont know how to change the datatype to ints. The error says "The operator + is undefined for the argument type(s) long, Object." This occurs at the first place I use an operator where I'm adding up all the elements to average them. If I block that line it happens to the next line so its going to be a problem with pretty much all of my code. Does anyone know how to deal with this? I feel like I cant be the first person to have this problem.
Heres my code for good measure. Its not working as it is but with some quick changes back to arrays it will calculate standard deviation so thats cool. Theres some extra stuff in there which is just there because I just wrote this code and am trying some different things.
- int elements = 300;
- ArrayList number, squares;
- long total = 0;
- long totalSquares = 0;
- long average, averageSquare;
- int standardDeviation;
- int counter = 0;
- void setup()
- {
- size(100,100);
- fill(255);
- number = new ArrayList();
- squares = new ArrayList();
- /*
- number.add(200);
- number.add(200);
- number.add(200);
- number.add(200);
- number.add(220);
- number.add(250);
- number.add(900);
- number.add(900);
- number.add(900);
- number.add(960);
- for (int i = 10; i < elements; i++) number.add(new int(random(480,520))); */
- for (int i = 0; i < elements; i++) number.add(int(random(480,520)));
- }
- void draw()
- {
- squares.clear();
- for (int i = 0; i < number.size(); i++) total = total + number.get(i);
- average = total / number.size();
- for (int i = 0; i < number.size(); i++) squares.add(i,(number.get(i) - int(average))*(number.get(i) - int(average)));
- for (int i = 0; i < number.size(); i++) totalSquares = totalSquares + squares.get(i);
- averageSquare = totalSquares / square.size();
- standardDeviation = int(sqrt(averageSquare));
- for (int i = 0; i < numbers.size(); i++){
- // if (number[i] < average - int(1.5 * standardDeviation) || number[i] > average + int(1.5 * standardDeviation)){
- if (number.get(i) < average - int(1 * standardDeviation) || number.get(i) > average + int(1 * standardDeviation)){
- number.remove(i);
- counter++;
- }
- }
- println("Standard Deviation " + standardDeviation);
- println("Average " + average);
- println("Number Passed " + counter);
- for (int i = 0; i < number.size(); i++){
- println(number.get(i));
- }
- while(true);
- }
1