ArrayList.toArray() 未转换为正确的类型?

发布于 2024-12-11 17:58:55 字数 633 浏览 0 评论 0原文

所以,我得到的是:

class BlahBlah
{
    public BlahBlah()
    {
        things = new ArrayList<Thing>();
    }

    public Thing[] getThings()
    {
        return (Thing[]) things.toArray();
    }

    private ArrayList<Thing> things;
}

在另一个类中我得到:

for (Thing thing : someInstanceOfBlahBlah.getThings())
{
    // some irrelevant code
}

错误是:

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [LsomePackage.Thing;
at somePackage.Blahblah.getThings(Blahblah.java:10)

我怎样才能解决这个问题?

So, what I got is:

class BlahBlah
{
    public BlahBlah()
    {
        things = new ArrayList<Thing>();
    }

    public Thing[] getThings()
    {
        return (Thing[]) things.toArray();
    }

    private ArrayList<Thing> things;
}

In the other class I got:

for (Thing thing : someInstanceOfBlahBlah.getThings())
{
    // some irrelevant code
}

And the error is:

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [LsomePackage.Thing;
at somePackage.Blahblah.getThings(Blahblah.java:10)

How can I solve this problem?

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

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

发布评论

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

评论(4

难以启齿的温柔 2024-12-18 17:58:55

尝试:

public Thing[] getThings()
{
    return things.toArray(new Thing[things.size()]);
}

您的原始版本不起作用的原因是 toArray() 返回 Object[] 而不是 Thing[]。您需要使用另一种形式的 toArray -- toArray(T[]) -- 获取 Things 数组。

Try:

public Thing[] getThings()
{
    return things.toArray(new Thing[things.size()]);
}

The reason your original version doesn't work is that toArray() returns Object[] and not Thing[]. You need to use the other form of toArray -- toArray(T[]) -- to get an array of Things.

得不到的就毁灭 2024-12-18 17:58:55

尝试

private static final Thing[] NO_THING = {};

return (Thing[]) things.toArray(NO_THING);

Try

private static final Thing[] NO_THING = {};

and

return (Thing[]) things.toArray(NO_THING);
捂风挽笑 2024-12-18 17:58:55

2 个 toArray() 函数。

    Object[] toArray()
        Returns an array containing all of the elements in this list in the correct order.
    <T> T[] toArray(T[] a)
        Returns an array containing all of the elements in this list in the correct order;
        the runtime type of the returned array is that of the specified array.

您正在使用第一个,它应该返回 Object[] 并且它正在这样做。如果您想获得正确的类型,请使用第二个版本:

things.toArray(new Thing[things.length]);

或者如果您不想在 new Thing[things.length] 上浪费更多空间,则只需将循环更改为强制转换:

Thing thing = null;
for (Object o : someInstanceOfBlahBlah.getThings())
{
    thing = (Thing) o;
    // some unrelevant code
}

There are 2 toArray() functions.

    Object[] toArray()
        Returns an array containing all of the elements in this list in the correct order.
    <T> T[] toArray(T[] a)
        Returns an array containing all of the elements in this list in the correct order;
        the runtime type of the returned array is that of the specified array.

You're using the first one, which is supposed to return Object[] and it's doing so. If you want to get correct type use the second version:

things.toArray(new Thing[things.length]);

or if you don't want to waste any more space on new Thing[things.length] then just change the loop to cast:

Thing thing = null;
for (Object o : someInstanceOfBlahBlah.getThings())
{
    thing = (Thing) o;
    // some unrelevant code
}
鹿童谣 2024-12-18 17:58:55

尝试

things.toArray(new Thing[0])

列表。 toArray javadoc

try

things.toArray(new Thing[0])

List.toArray javadoc

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