【问题标题】:List is a raw type. References to generic type List<E> should be parameterizedList 是一种原始类型。应参数化对泛型类型 List<E> 的引用
【发布时间】:2012-05-15 16:40:15
【问题描述】:

下面是我的语法

List synchronizedpubliesdList = Collections.synchronizedList(publiesdList);

我收到以下语法错误:

List is a raw type. References to generic type List&lt;E&gt; should be parameterized.

请提出解决方案。

【问题讨论】:

  • 这是一个警告,而不是错误。这行代码会编译,但 javac 不会做所有的类型检查。

标签: java generics types


【解决方案1】:

我相信

List 是原始类型。对泛型 List 的引用应该被参数化

不是错误,而是警告。

如果您打算使用 Java,了解泛型是基石,因此我建议您查看 Java 的教程页面:

java generics tutorials

因此,如果您知道publiesdList 中包含什么类型的对象,那么您可以这样做:

List<YourType> synchronizedpubliesdList = Collections.synchronizedList(publiesdList);

如果列表中有多种类型的对象,则可以使用通配符:

List<?> synchronizedpubliesdList = Collections.synchronizedList(publiesdList);

或者,如果您只是想摆脱警告而不是像这样抑制它:

@SuppressWarnings("rawtypes")
List synchronizedpubliesdList = Collections.synchronizedList(publiesdList);

但不推荐后者。

【讨论】:

  • 完美。我通过将 Generic 添加到声明中并将 Generic() 添加到代码中的实例化来解决了同样的问题。谢谢。
【解决方案2】:

你需要给它正确的泛型类型,例如

List<String> publiesdList = ...
List<String> synchronizedpubliesdList = Collections.synchronizedList(publiesdList);

【讨论】:

    【解决方案3】:

    您可以这样定义“publiesdList”:

    List<String> publiesdList = new List<String>();
    

    警告将消失。

    【讨论】:

      【解决方案4】:

      我在 Eclipse 中也遇到过同样的警告,只需单击警告标志并选择将类型参数添加到 Hash、List、Array 或您拥有的其他内容中。 大讨论列表在这里What is a raw type and why shouldn't we use it?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多