首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >类型参数T使用Eclipse在<T> T[] toArray(T[] a)中隐藏类型T

类型参数T使用Eclipse在<T> T[] toArray(T[] a)中隐藏类型T
EN

Stack Overflow用户
提问于 2012-09-22 22:28:12
回答 2查看 8.8K关注 0票数 7

使用Eclipse4.2和Java 7,并尝试实现下面的列表接口方法,我得到了一个警告。

代码语言:javascript
运行
复制
public <T> T[] toArray(T[] a) {
    return a;

}

警告说:

类型参数T隐藏类型T

为什么?我怎么才能摆脱它?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-09-22 22:30:43

列表接口也是通用的。确保您没有在类中对泛型类型使用T。注意,在http://docs.oracle.com/javase/6/docs/api/java/util/List.html中,类泛型参数使用"E“,toArray()泛型参数使用"T”。这样可以防止重叠。

代码语言:javascript
运行
复制
public class MyList<T> implements List<T> {

// V1 (compiler warning)
public <T> T[] toArray(T[] array) {
    // in this method T refers to the generic parameter of the generic method
    // rather than to the generic parameter of the class. Thus we get a warning.
    T variable = null; // refers to the element type of the array, which may not be the element type of MyList
} 

// V2 (no warning)
public <T2> T2[] toArray(T2[] array) {
    T variable = null; // refers to the element type of MyList
    T2 variable2 = null; // refers to the element type of the array
}

}

票数 13
EN

Stack Overflow用户

发布于 2015-10-22 21:51:58

另一个选项是,您有一个名为"T“的类的导入,这就是为什么要收到警告的原因。我刚刚解决了我的问题后,我发现我有一个无用的导入:

代码语言:javascript
运行
复制
org.apache.poi.ss.formula.functions.T

医生:检查你的进口品!

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

https://stackoverflow.com/questions/12548205

复制
相关文章

相似问题

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