首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Java:泛型方法和数字

Java:泛型方法和数字
EN

Stack Overflow用户
提问于 2010-10-04 02:18:34
回答 4查看 7.2K关注 0票数 16

我想做一个泛型方法,使一个数字的List的总和。

我尝试的是这样的:

代码语言:javascript
复制
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

我该怎么做呢?

谢谢

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3850970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档