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
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…