我不确定我的代码有什么问题......
public Vector blob (Pixel px)
{
Vector v = new Vector();
Point p = new Point(px.getX(), px.getY());
v.add(p);
return v;
}
Run Code Online (Sandbox Code Playgroud)
我得到以下内容:警告:[未选中]未选中调用添加(E)作为原始类型Vector的成员
v.add(P);
其中E是一个类型变量:E扩展在Vector类中声明的Object.
通过API查看,add函数将一个对象作为一个param,我之前清楚地说明了这一点,想法?
从Java-5开始,java.util.Vector<E>是一个通用容器,意思是(用非常简单的术语),您可以指定要存储的元素的类型,并让编译器自动检查该类型.在没有元素类型的情况下使用它是可以的,但它会触发警告.
这应该摆脱警告,并提供额外的类型安全检查:
public Vector<Point> blob (Pixel px) {
Vector<Point> v = new Vector<Point>();
Point p = new Point(px.getX(), px.getY());
v.add(p);
}
Run Code Online (Sandbox Code Playgroud)
关于向量的另一个注意事项是它们是同步容器.如果您不打算使用它们同步的事实,那么使用ArrayList<E>容器可能会更好.
泛型!!!。您必须为向量的内容指定对象类型。要删除警告,矢量代码应该是这样的
Vector<Point> v=new Vector<Point>()
Run Code Online (Sandbox Code Playgroud)