java - 为什么@SuppressWarnings 会破坏我的代码?

标签 java generics suppress-warnings

从上一个问题中得到这个想法。

How to create a generic array in Java?

无论如何,我的代码是这样的:

public class Slice<E>
{
    private E[] data;
    public Slice(Class<E> elementType, int size)
    {
        //@SuppresWarnings({"unchecked"})
        data = (E[])Array.newInstance(elementType, size);
    }

}

我删除了不需要的东西。当 suppress 指令被注释掉时,这编译得很好。当我取消注释时,我得到

Error: <identifier> expected    
        data = (E[])Array.newInstance(elementType, size);
             ^

有什么想法吗?为什么会这样?

最佳答案

您不能在此处放置注释。它必须位于 public 关键字之前。您还输入了错误的注释名称:将 SuppresWarnings 更改为 SuppressWarnings

编辑:如果您使用的是像 Eclipse 这样的 IDE,您通常会使用自动更正功能来插入注释。自然地,它会被插入到正确的位置并正确拼写。

关于java - 为什么@SuppressWarnings 会破坏我的代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1269717/

相关文章:

c - 是否有 gcc 命令行选项来消除警告 : passing argument n discards qualifiers from type

java - Junit多线程

java - 使用迭代值增量的趋势分析

C# lambda 表达式作为函数参数

c# - 如何在 conversionType 为十进制且输入为 "40.00"时使用 Convert.ChangeType()

delphi - 如何暂时禁用 "return value might be undefined"警告?

python - 如何使用 warnings.simplefilter() 忽略 SettingWithCopyWarning?

java - JSONException : Object has been collected Cannot evaluate JSONObject. toString()

java - 如何实时生成 citibike 站提要?

c# - 泛化方法和要传递的参数