# Calculate the average of an ArrayList

David Barry
Ranch Hand
Posts: 85
Good Morning everybody. I have the following method that uses an ArrayList to store electricity usage data. Since I'm fairly new to ArrayList's, I am wondering how I can calculate the average of the values in this ArrayList. I tried: , but that didn't work. Can somebody enlighten me here? Thanks

Larry Frissell
Ranch Hand
Posts: 82
2
You are sending an arraylist to the method, then creating a new arraylist. I think you want to take the arraylist monthlybill and "get" the values, then add these to a local Double variable.

fred rosenberger
lowercase baba
Bartender
Posts: 12228
36
you have a few options. you could keep a running total of all the values in the array as you insert them. When you want to calculate the average, get the size (the number of elements in the array), then use those for your division.

If for some reason you can't keep a running total, you'll have to iterate through your array and calculate the total on the fly. you can also easily count the number of elements here at the same time, and then do your division.

David Barry
Ranch Hand
Posts: 85
Thank you, Larry and Fred. However, as I am still new to ArrayLists, I am not sure how to keep a running total or iterate through the array. I have a few ideas; however, I can't get them to materialize.

Garrett Rowe
Ranch Hand
Posts: 1296
Do you know how to use a "for" loop?

David Barry
Ranch Hand
Posts: 85
Yes

David Barry
Ranch Hand
Posts: 85
Will that iterate through the array and calculate the total?

David Barry
Ranch Hand
Posts: 85
Would something like this work?

Rob Reel
Greenhorn
Posts: 3

[ Jesper Young: Solution deleted. Please let people do their own homework. ]

Garrett Rowe
Ranch Hand
Posts: 1296
This line:

Isn't doing anything close to what so seem to believe it is. An ArrayList is not an array. It has no concept of the square bracket notation [] to retrieve an element at a particular index. Instead you must call a method on the ArrayList to get the value at a particular index. Also you seem to be familiar with the what the ++ notation does since you used it in the update argument to you for loop, is it really what you want to use in this situation?

If someone gave you a pile of index cards each with a different number on them how would you go about calculating the average number?

How would you translate that into Java code?

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15556
43
David Barry wrote:Would something like this work?

Can you explain what that code is supposed to do? How do you think that those lines of code are going to compute the average of the numbers in the list for you?

Instead of trying things without understanding exactly what you're doing, take smaller steps and make sure that you understand each step before you proceed with the next.

I guess you know what you need to do to compute the average of a list of numbers: add up all the numbers, and then divide the sum by the number of numbers in the list. Think about how you would do that in Java code: you would need to loop through all the numbers in the list, adding them up in each iteration in the list. After that you have to divide the result by the number of items in the list.

David Barry
Ranch Hand
Posts: 85
Thanks for all of the thought-provoking questions. I now better understand how to get the sum of the values of the ArrayList. However, now I have happened upon a somewhat different problem. In the Tester class, I am trying to print out the average monthly electric bill value. However, when I try to run it, I get an error stating: "calcAveragePrice(java.util.ArrayList<java.lang.Double>in CO2FromElectricitycannot be applied to()" I am wondering how to overcome this.

Both classes that make up this program are shown below:

Garrett Rowe
Ranch Hand
Posts: 1296
In your CO2FromElectricity class you have this:

So your calcAveragePrice takes a parameter which is an ArrayList containing the monthly price of electricity per kilowatthour, but where is this ArrayList when you try to call the method from the main() method?

David Barry
Ranch Hand
Posts: 85
Yes, thank you. I thought I should pass a parameter when I call the method back up in the main method; however, I don't know parameter to pass to it because I have already done the entire calculation in the method.

Garrett Rowe
Ranch Hand
Posts: 1296
Maybe then your calcAverageBill method should not create and add values to an ArrayList within the method, instead it should only work with the ArrayList passed as a parameter. Curiously, if both your calcAveragePrice and calcAverageBill method adopted this strategy, then they would look exactly alike. If that happens maybe you could eliminate one of them and rename the other appropriately, perhaps calculateAverage(ArrayList<Double> alist).

David Barry
Ranch Hand
Posts: 85
Then if I do not create and add values to an ArrayList within the method, am I going to do that in the main method?

David Barry
Ranch Hand
Posts: 85
Thanks for the tips, Garrett. I believe that I have made some progress. However, I am getting output like this: [ CO2FromElectricityTester.main({ }) ]
Average Monthly Electricity Bill: 0.000000

...so something must still be wrong. Although seems to me it should work by now...

Garrett Rowe
Ranch Hand
Posts: 1296
Look here:

Why would monthly total still be 0.0 when you get to the return statement?