我想做一个泛型方法,使一个数字的List
的总和。
我尝试的是这样的:
public static <T extends Number> T sumList(List<T> data)
{
T total = 0;
for (T elem : data)
{
total += elem;
}
return total;
}
但问题是there is no += operator in T
和那个total can't be assigned to zero
。
我该怎么做呢?
谢谢
https://stackoverflow.com/questions/3850970
复制相似问题