Java 将对象数组转换为整数数组错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1115230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
casting Object array to Integer array error
提问by Ross
What's wrong with the following code?
以下代码有什么问题?
Object[] a = new Object[1];
Integer b=1;
a[0]=b;
Integer[] c = (Integer[]) a;
The code has the following error at the last line :
代码在最后一行有以下错误:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer;
线程“main”中的异常 java.lang.ClassCastException: [Ljava.lang.Object; 不能转换为 [Ljava.lang.Integer;
采纳答案by namalfernandolk
Ross, you can use Arrays.copyof() or Arrays.copyOfRange() too.
罗斯,你也可以使用 Arrays.copyof() 或 Arrays.copyOfRange()。
Integer[] integerArray = Arrays.copyOf(a, a.length, Integer[].class);
Integer[] integerArray = Arrays.copyOfRange(a, 0, a.length, Integer[].class);
Here the reason to hitting an ClassCastException
is you can't treat an array of Integer
as an array of Object
. Integer[]
is a subtype of Object[]
but Object[]
is not a Integer[]
.
这里击中 an 的原因ClassCastException
是您不能将数组Integer
视为Object
. Integer[]
是 的子类型,Object[]
但Object[]
不是Integer[]
。
And the following also will not give an ClassCastException
.
而以下也不会给出ClassCastException
.
Object[] a = new Integer[1];
Integer b=1;
a[0]=b;
Integer[] c = (Integer[]) a;
回答by Sean
You can't cast an Object
array to an Integer
array. You have to loop through all elements of a and cast each one individually.
您不能将Object
数组强制转换为Integer
数组。您必须遍历 a 的所有元素并单独投射每个元素。
Object[] a = new Object[1];
Integer b=1;
a[0]=b;
Integer[] c = new Integer[a.length];
for(int i = 0; i < a.length; i++)
{
c[i] = (Integer) a[i];
}
Edit: I believe the rationale behind this restriction is that when casting, the JVM wants to ensure type-safety at runtime. Since an array of Objects
can be anything besides Integers
, the JVM would have to do what the above code is doing anyway (look at each element individually). The language designers decided they didn't want the JVM to do that (I'm not sure why, but I'm sure it's a good reason).
编辑:我相信此限制背后的基本原理是,在强制转换时,JVM 想要确保运行时的类型安全。由于数组Objects
可以是除 之外Integers
的任何东西,JVM 无论如何都必须执行上述代码正在执行的操作(单独查看每个元素)。语言设计者决定他们不希望 JVM 这样做(我不确定为什么,但我确定这是一个很好的理由)。
However, you can cast a subtype array to a supertype array (e.g. Integer[]
to Object[]
)!
但是,您可以将子类型数组转换为超类型数组(例如Integer[]
to Object[]
)!
回答by Tommaso Taruffi
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer;
you try to cast an Array of Object to cast into Array of Integer. You cant do it. This type of downcast is not permitted.
您尝试将对象数组转换为整数数组。你不能这样做。这种类型的向下转型是不允许的。
You can make an array of Integer, and after that copy every value of the first array into second array.
您可以创建一个整数数组,然后将第一个数组的每个值复制到第二个数组中。
回答by Michael
Or do the following:
或执行以下操作:
...
Integer[] integerArray = new Integer[integerList.size()];
integerList.toArray(integerArray);
return integerArray;
}
回答by S R Chaitanya
当涉及到 Object 类型的转换时,
instanceof
instanceof
测试应该通过以便分配通过。在您的示例中,结果Object[] a = new Object[1];
boolean isIntegerArr = a instanceof Integer[]
Object[] a = new Object[1];
boolean isIntegerArr = a instanceof Integer[]
如果您执行
sysout
sysout
上述行中的 a,它将返回 false;
所以在投射之前尝试一个检查实例会有所帮助。因此,要修复错误,您可以添加“instanceof”检查
或
使用以下代码行: 请注意,如果 Object 数组包含除 Integer 以外的任何条目,上述代码将失败。
(Arrays.asList(a)).toArray(c);
(Arrays.asList(a)).toArray(c);