克隆向量时无法摆脱 Java 警告

发布于 2024-09-19 12:30:29 字数 605 浏览 2 评论 0原文

我有两个向量被声明为私有类属性:

private Vector<Myobject> v1 = new Vector<Myobject>();
private Vector<Myobject> v2 = new Vector<Myobject>();

我用一堆 Myobject 填充 v1。

我需要将 v1 浅克隆到 v2。我尝试过:

v2 = v1.clone();

我得到“未经检查或不安全的操作”。

我尝试了所有形式的转换,但似乎无法克服这个警告。

即使我删除第二个(v2)声明并尝试克隆:

Vector<Myobject> v2 = v1.clone();

Vector<Myobject> v2 = ( Vector<Myobject> ) v1.clone();

...仍然得到它。

我确信我在这里遗漏了一些非常基本的东西......

提前致谢

I have two vectors declared as a private class property:

private Vector<Myobject> v1 = new Vector<Myobject>();
private Vector<Myobject> v2 = new Vector<Myobject>();

I fill up v1 with a bunch of Myobjects.

I need to do a shallow clone of v1 to v2. I tried:

v2 = v1.clone();

I get "unchecked or unsafe operations".

I've tried all forms of casting and cannot seem to overcome this warning.

Even if I remove the second (v2) declaration and try and just clone:

Vector<Myobject> v2 = v1.clone();

or

Vector<Myobject> v2 = ( Vector<Myobject> ) v1.clone();

... still get it.

I'm sure I'm missing something very basic here...

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何时共饮酒 2024-09-26 12:30:29

当将非参数化类型(例如 clone() 返回的 Object)转换为参数化类型时,编译器总是会发出警告。这是因为目标类型 Vector 不仅保证其自身,还保证其中包含的对象。但是,无法在运行时验证这些保证,因为类型参数信息已被删除。

请参阅此处获取更详细的说明。

如前所述,如果您只是尝试复制向量 v1,正确的方法是使用复制构造函数。

Vector<Myobject> v2 = new Vector<Myobject>(v1);

生成的克隆将是浅克隆,因为这仅将 Myobject 引用从 v1 复制到 v2。

The compiler will always give a warning when casting a non-parametrized type (such as the Object returned by clone()) into a parametrized one. This is because the target type Vector<Myobject> is making guarantees about not only itself, but also objects contained within it. However there's no way to verify those guarantees at runtime because the type parameter information is erased.

See here for a a more detailed explanation.

As mentioned earlier, if you're just trying to make a copy of the vector v1, the proper way to do that is to use the copy constructor.

Vector<Myobject> v2 = new Vector<Myobject>(v1);

The resulting clone will be shallow as this only copies the Myobject references from v1 to v2.

爱人如己 2024-09-26 12:30:29

尝试使用复制构造函数:

Vector<Myobject> v2 = new Vector<Myobject>(v1);

Try using the copy constructor instead:

Vector<Myobject> v2 = new Vector<Myobject>(v1);
超可爱的懒熊 2024-09-26 12:30:29

有时,作为程序员,您知道警告不是问题,但是无法仅通过修改代码来说服编译器。这就是为什么它是警告而不是错误。

在这些情况下,您可以在作业上方添加 SuppressWarnings 注释:

@SuppressWarnings("unchecked")
Vector<Myobject> v2 = (Vector<Myobject>) v1.clone();

Sometimes, you as a programmer know that the warning is not a problem, however there is no way to convince the compiler simply by modifying code.. this is why it's a warning and not an error.

In these cases, you can add a SuppressWarnings annotation above your assignment:

@SuppressWarnings("unchecked")
Vector<Myobject> v2 = (Vector<Myobject>) v1.clone();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文