在转换为超类列表时,Java 中未检查的转换

发布于 2024-10-08 21:09:27 字数 375 浏览 0 评论 0原文

在我的 Java 程序中,我需要转换 List 扩展为 List。如果我直接进行强制转换,编译器会抱怨(显示警告)存在未经检查的强制转换。尽管我可以确定所有对象都可以转换为其超类,但编译器不会注意到它并报告未经检查的强制转换。我怎样才能摆脱警告?除了@SupressWarnings(“unchecked”)还有其他方法吗?

PS:代码工作正常,我只是好奇是否有更好的方法。

编辑:

解决方案:只有当他确定将来不会更改列表时,才应该进行这种类型的转换。但是,当我们将单个对象从列表中取出时,最好对它们进行强制转换。

In my Java program, I need to cast an object of type List<? extends SomeBaseClass> to List<SomeBaseClass>. If I cast it directly the compiler complains (shows a warning) that there is an unchecked cast. Even though I can be sure that all the objects can be converted to their superclass, the compiler does not notice it and reports an unchecked cast. How can I get rid of the warning? Is there any other way than @SupressWarnings("unchecked")?

P.S.: The code works fine, I am just curious whether there is a better way of doing things.

Edit:

Solution: One should only do this type of cast if he is sure the he will not change the list in the future. But it is better to cast individual objects when we take them out of the list.

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

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

发布评论

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

评论(3

千秋岁 2024-10-15 21:09:27

编译器会抱怨,因为如果您将 SomeBaseClass 类型的对象添加到 List 列表中,则可能会“违反”List 列表。

这里有一个例子,当 Number 代表你的 SomeBaseClass 时:

List<? extends Number> doubles = new ArrayList<Double>();
List<Number> nums = (List<Number>) doubles;

nums.add(new Integer(5));    // no compiler complaints here...

// doubles now contains an Integer value!

如果你的情况没有办法解决这个问题,我相信 @SuppressWarnings("unchecked")< /code> 是你最好的选择。

The complier complains since if you add an object of type SomeBaseClass to your List<SomeBaseClass> list, you may "violate" the content of your List<? extends SomeBaseClass> list.

Here in an example when Number figures as your SomeBaseClass:

List<? extends Number> doubles = new ArrayList<Double>();
List<Number> nums = (List<Number>) doubles;

nums.add(new Integer(5));    // no compiler complaints here...

// doubles now contains an Integer value!

If there is no way around this in your case, I believe the @SuppressWarnings("unchecked") is your best option here.

伤痕我心 2024-10-15 21:09:27

你试图做的事情是不安全的。考虑这个简单的例子:

import java.util.*;

class SomeBaseClass
{
}

class SomeSubClass extends SomeBaseClass
{
    public static void main(String[] a)
    {
        List<SomeSubClass> orig = new ArrayList<SomeSubClass>();
        // Compiles with no warnings.  This is the purpose of the ? extends syntax
        List<? extends SomeBaseClass> l1 = orig;
        // This is what you're trying to do
        List<SomeBaseClass> l2 = (List<SomeBaseClass>) l1;
        // Then, we add a SomeBaseClass to the new list
        l2.add(new SomeBaseClass());
        // ClassCastException, since this casts a SomeBaseClass to a SomeSubClass
        SomeSubClass first = orig.get(0);
    }
}

What you're trying to do is unsafe. Consider this simple example:

import java.util.*;

class SomeBaseClass
{
}

class SomeSubClass extends SomeBaseClass
{
    public static void main(String[] a)
    {
        List<SomeSubClass> orig = new ArrayList<SomeSubClass>();
        // Compiles with no warnings.  This is the purpose of the ? extends syntax
        List<? extends SomeBaseClass> l1 = orig;
        // This is what you're trying to do
        List<SomeBaseClass> l2 = (List<SomeBaseClass>) l1;
        // Then, we add a SomeBaseClass to the new list
        l2.add(new SomeBaseClass());
        // ClassCastException, since this casts a SomeBaseClass to a SomeSubClass
        SomeSubClass first = orig.get(0);
    }
}
送你一个梦 2024-10-15 21:09:27

你情绪低落,这是一种不好的做法。尝试重构您的代码以避免这种转换。当然,在这种情况下您不应该抑制警告。

You're downcasting, which is a bad practice. Try to refactor your code to avoid this casting. And of course you should not suppress warnings in this case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文