Hi Guys , I am rather new to Java and I am trying to extend the ArrayList object to that when an ArrayList of Integers exists it has a function to return the Average value of the Integers within the ArrayList. Below is the code I have so far:
import java.util.*;*
*import java.lang.*;
publicclass MathArrayList<Integer> extends ArrayList<Integer> {
double average;
publicdouble Average() {
CalculateAverage();
return average;
}
privatevoid CalculateAverage() {
double total = 0;
for(int i = 0; i < size(); i++) {
total += get(i).intValue();
}
average = total / size();
}
}
When I attempt to compile this code I get: Cannot find symbol - Line 18 - ".intValue()" and I cannot work out why! I have created a smaller java project to test that I am using the method '.intValue()' correctly and it is converting and Integer to an int fine! Why cannot it not find a symbol?
// Method names should not start with capital letters
privatevoid calculateAverage() {
double total = 0;
for( final Integer value : this ) {
total += value; // You can autounbox if you're confident the List will never contain nulls.
}
average = total / size();
}
My inclination would be to implement this as an external function however:
publicstaticdouble calculateCollectionAverage(final Collection<Integer> collection) {
double total = 0;
for( final Integer value : collection ) {
total += value;
}
return (total / collection.size());
}
You should note that none of these approaches is threadsafe though.
dcminter wrote:
get returns an Object not the parameterized type. Use an iterator instead.
Eh? ArrayList<E>'s get method is:
E get(int index);
So if E is Integer, get returns Integer, right?
@OP: I don't believe that's the actual code you're trying to compile.
If it is, there are some other issues with it.
#1: There's no reason to extend ArrayList for this. You aren't creating a special kind of ArrayList - you should just use an ArrayList and create a method that operates on that ArrayList to return the average. Favor composition over inheritance.
#2: Your calculate average method is not thread safe. You're calculating a member variable and then returning it, leaving room for another thread to alter that member variable in-between and get the wrong result. It should just calculate and return the average by using a local method variable. No need for the member variable there.
stew123dai wrote:
Hi Guys , I am rather new to Java and I am trying to extend the ArrayList object to that when an ArrayList of Integers exists it has a function to return the Average value of the Integers
I strongly advise against that. A List's job is to maintain its elements in the order they're added. Adding responsibilities like computing the average is not good OO.
Thanks for all the responses guys, I have tried using your code dcminter and it gives me the same error I was getting before I tried using the '.intValue' method. That is that the '+' operator cannot be applied to Integer. I have researched this before and am sure this is because an object Integer is returned and not an int. I wanted to created what I called a MathArrayList as i wanted to add more functions such as StandardDev. extra to the list of methods so i could use this new ArrayList within many of the project I have to create. The import java.lang.*; is only in there as I was trying to work out why the method could not be found. I still haven't found a direct solution to my problem and still need an object which can hold 'int's.
After reading all of the comment I have decided to try and complete this project in a different way. Thanks very much for all the help !
Re: Extending ArrayList Object - Help Needed!
Nov 4, 2009 2:17 AM
(reply 10
of 10) (In reply to
#9 )
That is that the '+' operator cannot be applied to Integer. I have researched this before and am sure this is because an object Integer is returned and not an int.
In that case you're running an old version of Java (or of the JDK at least). Generics and auto(un)boxing weren't introduced until Java 5. Upgrade.