wood burning stoves 2.0*
The moose likes Java in General and the fly likes How to find the average of  arraylist containing database values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to find the average of  arraylist containing database values" Watch "How to find the average of  arraylist containing database values" New topic
Author

How to find the average of arraylist containing database values

partha phukan
Ranch Hand

Joined: Mar 13, 2011
Posts: 31
Hi...! Can anyone help me to get out of my problem. I could not able to find the average of an arraylist which contains database values. I am using java swing.

i have a database table with 4 fields. i have stored the values from the table using getDouble() method in 4 separate arraylist. Now i need to calculate the average of those 4 arraylist separately and display in 4 Jlabels.

I have 2 classes- one class where i declare the database connections and another class where i need to display the returned values in JLabels by calling the appropriate method of the database class.

Now, where i need to do the average calculation- whether in the database class or in the class where i display the JLabels? What to return from the method in the database class?

i don't know whether i could able to explain you properly or not. Sorry, for my english.

Thank you in advance.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10270
    
    8

How about a separate helper class? It would have a static method which would accept the List of doubles and return the average as a double?
This question is not really related to GUIs. I will move it to a more appropriate forum.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
partha phukan
Ranch Hand

Joined: Mar 13, 2011
Posts: 31
Thank you sir for your suggestion.

But "a class with static method which would accept the List of doubles"- i am not fully understood about this line. how to accept as the list of doubles? whether i have to declare another arraylist and add all the 4 arraylist and return the new arraylist from the method of the database class?

again you said that "return the average as a double from the separate helper class"- whether it will return separately the 4 average value?

Please clear me. I am not sure how to proceed. Thank you once again.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10270
    
    8

1) Do you know how to write methods? Just declare them as static.
2) Do you know what method arguments are? You need to pass your list as an argument to the method
3) You need to find the average of data in a list right? You got four lists so you should end up with 4 different doubles. So you need to call this method 4 times and pass the 4 different lists to it and get back 4 different average values
 
Don't get me started about those stupid light bulbs.
 
subject: How to find the average of arraylist containing database values