I want to make a generic method which makes the total sum of a List of numbers.
What I was trying is this:
public static <T extends Number> T sumList(List<T> data)
{
T total = 0;
for (T elem : data)
{
total += elem;
}
return total;
}
But the problem is that there is no += operator in T and that total can't be assigned to zero.
How can I do this?
Thanks