java 警告:[rawtypes] 发现原始类型:DefaultListModel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7823140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
warning: [rawtypes] found raw type: DefaultListModel
提问by Sam
I'm getting this warning from the compiler which does not make sense at all (at least to me). It basically wants me to assign a Type to DefaultListModel which is a object type by itself! I'm getting heaps of this warnings all through my code!
我从编译器那里收到这个警告,这根本没有意义(至少对我来说)。它基本上希望我为 DefaultListModel 分配一个类型,它本身就是一个对象类型!我在我的代码中收到了很多这样的警告!
C:\Documents and Settings\...\filename.java:345:warning: [rawtypes] found raw type: DefaultListModel
DefaultListModel lm = (DefaultListModel) jList_DataSetList.getModel();
missing type arguments for generic class DefaultListModel<E>
where E is a type-variable:
E extends Object declared in class DefaultListModel
This is another one which I have no idea where is comes from!
这是另一个我不知道从哪里来的!
C:\Documents and Settings\...\filename.java:897: warning: [rawtypes] found raw type: JList
private javax.swing.JList jList_DataSetList;
missing type arguments for generic class JList<E>
where E is a type-variable:
E extends Object declared in class JList
Thanks in advance
提前致谢
回答by JB Nizet
Since Java 7, DefaultListModel
is a generic type, like List
, Set
, etc. It expects a type : DefaultListModel<SomeClass>
instead of the raw DefaultListModel
.
由于Java 7,DefaultListModel
是一个通用的类型,比如List
,Set
等它需要一个类型:DefaultListModel<SomeClass>
替代原料DefaultListModel
。
This allows to work in a more type-safe way, because you won't be able to insert a String into a list model which is supposed to contain Integer instances. And you won't have to cast to Integer when getting an element from the model.
这允许以更类型安全的方式工作,因为您将无法将 String 插入到应该包含 Integer 实例的列表模型中。从模型中获取元素时,您不必强制转换为 Integer。
The same is true for JList
which is also now a JList
of Strings or a JList
of Integers, instead of being a raw JList
.
对于JList
现在JList
也是字符串的 a 或JList
整数的a也是如此,而不是原始的JList
。
Read the tutorial about generics, and have a look at the javadoc of DefaultListModel.
阅读关于泛型的教程,并查看DefaultListModel的javadoc。
回答by user1454473
Try @SuppressWarnings("rawtypes")
试试@SuppressWarnings("rawtypes")
This is a particularly awful default when working with reflection or when working with interfaced methods where the exact type of the generic object is supposed to be hidden. I started changing my Class references to Class when I remembered @Suppress.
当使用反射或使用接口方法时,这是一个特别糟糕的默认值,其中泛型对象的确切类型应该被隐藏。当我想起@Suppress 时,我开始将我的 Class 引用更改为 Class。
回答by Martin Vondrá?ek
And if someone is looking for these warnings in the NetBeans IDE (7.0.1) generated code (for JList etc.) and you don't know, how to correct it, follow these steps:
如果有人在 NetBeans IDE (7.0.1) 生成的代码(用于 JList 等)中查找这些警告,而您不知道如何更正,请按照以下步骤操作:
- Go to GUI editor (matisse builder).
- Select (click on) the component (JList, etc.)
- Go to Properties window, to Code tab
- Write the <Something> into Type Parametres property. Of course, the "Something" would be String, or your object...
- 转到 GUI 编辑器(matisse builder)。
- 选择(单击)组件(JList 等)
- 转到“属性”窗口,转到“代码”选项卡
- 将 <Something> 写入 Type Parameters 属性。当然,“东西”将是字符串,或者你的对象......