使用向量的 Java 警告:对 add(E) 的未检查调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1840590/
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
Java Warning using Vectors: unchecked call to add(E)
提问by kylex
Offending bit of code
有问题的代码
Vector moves = new Vector();
moves.add(new Integer(x));
Error:
错误:
ConnectFour.java:82: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.Vector moves.add(new Integer(x));
Not really sure how much info is needed for an error like this....
不确定这样的错误需要多少信息......
采纳答案by coobird
The problem is that the code above is not using generics.
问题是上面的代码没有使用泛型。
The following will work:
以下将起作用:
Vector<Integer> moves = new Vector<Integer>();
move.add(new Integer(x));
The type name inside the <>
(in the case of Vector
, the type parameter E
for the element to hold) tells the compiler what type of object it should expect.
中的类型名称<>
(在 的情况下Vector
,E
元素要保存的类型参数)告诉编译器它应该期望什么类型的对象。
If one tries to add an object that is of the specified type, such as in this case, trying to add an String
to and Vector<Integer>
, an compile-time error will occur, indicating that a type of object that is not of the expected type is being added.
如果尝试添加指定类型的对象,例如在这种情况下,尝试添加String
to and Vector<Integer>
,则会发生编译时错误,表明正在添加不是预期类型的对象类型添加。
That said, one should try not to use the Vector
class. For more purposes, a class implementing List
such as ArrayList
from the Java Collections Frameworkwould be sufficient, and better performing.
也就是说,应该尽量不使用Vector
该类。欲了解更多的目的,实现一类List
,如ArrayList
从Java集合框架就足够了,而且性能更好。
Edit
编辑
Although not directly related to the question about generics, Adam Paynter brought up a good point in the comments about the use of auto-boxing.
尽管与泛型问题没有直接关系,但 Adam Paynter 在关于使用自动装箱的评论中提出了一个很好的观点。
Since Java 5, primitives and their wrapper classes, e.g. int
and Integer
will be automatically converted between each other as necessary.
从 Java 5 开始,原语和它们的包装类,例如int
和Integer
将根据需要在彼此之间自动转换。
Therefore, it is possible to add an value specified as an int
or an int
literal into a class expecting an Integer
:
因此,可以将指定为 anint
或int
文字的值添加到需要 的类中Integer
:
Vector<Integer> v = new Vector<Integer>();
v.add(5); // Not necessary to use an Integer value.
回答by Andreas Dolk
That's not an error, it's just a compiler warning. Vector is usually parametized, so to get rid of the warning, just use generics:
这不是错误,只是编译器警告。Vector 通常是参数化的,因此要摆脱警告,只需使用泛型:
Vector<Integer> moves = new Vector<Integer>();
moves.add(new Integer(x));
回答by Bozho
initialize your vector like this
Vector<Integer> moves = new Vector<Integer>();
Preferably use
java.util.ArrayList
- it's a replacement ofVector
像这样初始化你的向量
Vector<Integer> moves = new Vector<Integer>();
最好使用
java.util.ArrayList
- 它是替代品Vector
回答by James Schek
If you have no choice but to use the non-generic data structure, you can put @SuppressWarnings("unchecked")
at the start of the method to silence the warning.
如果您别无选择,只能使用非通用数据结构,您可以@SuppressWarnings("unchecked")
在方法的开头放置以消除警告。
This only be done if you have no choice but to use the non-generic vector. This usually happens when you're working with older libraries or certain parts of the Java runtime libraries.
仅当您别无选择只能使用非通用向量时才这样做。当您使用较旧的库或 Java 运行时库的某些部分时,通常会发生这种情况。
回答by fikovnik
Not directly related to the code, but it is recommendedto use (from version >= 5):
与代码没有直接关系,但建议使用(从版本>= 5):
Integer.valueOf(x);
instead of
代替
new Integer(x);
Because, some integer values {-128,...,127) are cachedand it will always return the same object. This is very useful especially regarding to autoboxing.
因为,一些整数值 {-128,...,127) 被缓存并且它总是返回相同的对象。这对于自动装箱非常有用。