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

Der*_*ick 1 java generics suppress-warnings

从前一个问题得到了这个想法.

如何在Java中创建通用数组?

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

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

}
Run Code Online (Sandbox Code Playgroud)

我删除了不必要的东西.当抑制指令被注释掉时,这个编译很好.当我取消注释时,我得到了

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

有任何想法吗?为什么会发生这种情况?

Ste*_*n C 6

你不能在那里放一个注释.它必须在public关键字之前.而且您也错误地输入了注释名称:更改SuppresWarningsSuppressWarnings.

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