从对象向下转换为整数运行时错误:java.lang.ClassCastException

发布于 2024-11-24 14:13:38 字数 420 浏览 2 评论 0原文

运行时异常 - java.lang.ClassCastingException...

Integer intArr[] = new Integer[arrList.size()];
ArrayList <Integer> arrList =new ArrayList();
intArr=(Integer[])arrList.toArray(); // returns Object class which is downcaste to Integer;

我知道向下转换并不安全,但为什么会发生这种情况? 我还尝试将 ArrayList 转换为 StringIntegerint,但我得到了同样的错误。

Run time exception-- java.lang.ClassCastingException...

Integer intArr[] = new Integer[arrList.size()];
ArrayList <Integer> arrList =new ArrayList();
intArr=(Integer[])arrList.toArray(); // returns Object class which is downcaste to Integer;

I understand down-casting is not safe but why is this happening?
I also tried to converting ArrayList to String to Integer to int, but I get the same error.

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

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

发布评论

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

评论(4

热风软妹 2024-12-01 14:13:38

尝试这样做,

intArr = arrList.toArray(new Integer[arrList.size()]);

您得到的是一个类型化的Integer数组,而不是一个对象数组。

Try to do this

intArr = arrList.toArray(new Integer[arrList.size()]);

What you get is a typed Integer Array and not a Object array.

2024-12-01 14:13:38

首先,这不会将 ArrayList 绑定到 Integer 类型。

ArrayList <Integer> arrList =new ArrayList();

相反,这就是发生的情况,arrList 被分配给原始类型的 ArrayList,但这不是问题。

问题在于,

intArr=(Integer[])arrList.toArray();

由于 arrList 是原始类型(由于赋值,它被编译器分配为 new ArrayList()),因此您'而是有效地获取 Object[]

尝试将 arrList 分配给 new ArrayList() 并执行以下操作:

intArr = arrList.toArray(new Integer[arrList.size()]);

First of all, this doesn't bind the ArrayList to type Integer.

ArrayList <Integer> arrList =new ArrayList();

Instead, this is what happens, arrList is assigned to an ArrayList of raw type, but that isn't a problem.

The problem lies in,

intArr=(Integer[])arrList.toArray();

since arrList is a raw-type (due to the assignment, it gets assigned as new ArrayList<Object>() by the compiler), you're effectively getting an Object[] instead.

Try assigning arrList to new ArrayList<Integer>() and do this:

intArr = arrList.toArray(new Integer[arrList.size()]);
望她远 2024-12-01 14:13:38

这里的问题是您正在尝试将对象数组转换为整数数组。 Array 本身就是一个对象,Integer[] 不是 ArrayList 的子类,反之亦然。在您的情况下,您需要做的是转换单个项目,如下所示:

Integer intArr[] = new Integer[arrList.size()];
for(int i=0; i<intArr.length; i++)
{
    intArr[i] = (Integer)arrList.get(i);
}

当然,如果数组列表中的单个元素不是 Integer 类型,您可能会得到 ClassCastException。

The problem here is that you are trying to convert an array of objects to an array of integers. Array is an object in itself and Integer[] is not a sub-class of ArrayList, nor vice versa. What you have to do in your case is cast individual items, something like this:

Integer intArr[] = new Integer[arrList.size()];
for(int i=0; i<intArr.length; i++)
{
    intArr[i] = (Integer)arrList.get(i);
}

Naturally, you may get ClassCastException if individual elements in the array list are not of type Integer.

云之铃。 2024-12-01 14:13:38

toArray(T[] a) 接受一个参数:
“a - 要存储列表元素的数组(如果足够大);否则,是相同 runt 的新数组”

toArray(T[] a) takes a paramter:
"a - the array into which the elements of the list are to be stored, if it is big enough; otherwise, a new array of the same runt"

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