I have an array list of generic types and i'm trying to create a method to calculate the average, but I cannot add the a generic without casting it first but I'm suppose to pass in integers or double how can i do this?
private ArrayList<T> grades;
public double computeAverage()
{
double average = 0;
double sum = 0;
for (int i = 0; i < grades.size(); i++) {
sum = sum + grades.get(i); <<< ERROR
}
average = sum/grades.size();
return average;
}
Error: The operator + is undefined for the argument type(s) double, T
grades.Tdeclared at the class level? How do you instantiate the instance of this object?+operator with typeT.