I want to have a method which calculates the mean of a LinkedList of type Integer, Double and Float.
The problem is the sum += i;
statement, since java says that the + operator isn't defined for type Object.
I could do a cast, but if the LinkedList was of type Float, for example, and the cast was to Integer, I would be not computing the correct mean.
What should I do? Thanks.
public double mean (LinkedList<?> l)
{
double sum = 0;
int n = 0;
for (Object i : l)
{
n++;
sum += i;
}
return sum / n;
}