使用向量的Java警告:未选中的添加调用(E)
有问题的代码使用向量的Java警告:未选中的添加调用(E),java,vector,Java,Vector,有问题的代码 Vector moves = new Vector(); moves.add(new Integer(x)); 错误: 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)); 不确定这样的错误需要多少信息……问题是上面的代码没有使用 以下工作将起作用: Ve
Vector moves = new Vector();
moves.add(new Integer(x));
错误:
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));
不确定这样的错误需要多少信息……问题是上面的代码没有使用 以下工作将起作用:
Vector<Integer> moves = new Vector<Integer>();
move.add(new Integer(x));
这不是错误,只是编译器警告。向量通常是参数化的,所以要消除警告,只需使用泛型:
Vector<Integer> moves = new Vector<Integer>();
moves.add(new Integer(x));
向量移动=新向量();
移动。添加(新整数(x));
Vector<Integer> moves = new Vector<Integer>();
向量移动=新向量();
java.util.ArrayList
——它是Vector
如果您别无选择,只能使用非通用数据结构,则可以将
@SuppressWarnings(“unchecked”)
放在方法的开头,以使警告静音
只有当您别无选择,只能使用非泛型向量时,才能执行此操作。当您使用较旧的库或Java运行时库的某些部分时,通常会发生这种情况。与代码没有直接关系,但要使用它(从版本>=5): 而不是
new Integer(x);
因为,一些整数值{-128,…,127)被缓存,并且它将始终返回相同的对象。这对于+1非常有用。此外,它们似乎是为Java 5或更高版本编译的。因此,它们还可以利用自动装箱:
move.add(x)
注意,这不是一个错误,只是一个警告。如果你不在乎警告,那么执行上述操作仍然是完全有效的。
Integer.valueOf(x);
new Integer(x);