与ArrayStoreException信息处理 [英] Dealing with an ArrayStoreException
问题描述
Object[] o = "a;b;c".split(";");
o[0] = 42;
抛出
java.lang.ArrayStoreException: java.lang.Integer
,而
String[] s = "a;b;c".split(";");
Object[] o = new Object[s.length];
for (int i = 0; i < s.length; i++) {
o[i] = s[i];
}
o[0] = 42;
不
是否有任何其他方式来处理该异常,而无需创建一个临时的String []
数组?
Is there any other way to deal with that exception without creating a temporary String[]
array?
推荐答案
在Java中的数组也是一个的对象的
In Java an array is also an object.
您可以把一个的亚型的对象放入的超的变量。例如,你可以把一个字符串
对象转换为对象
变量。
You can put an object of a subtype into a variable of a supertype. For example you can put a String
object into an Object
variable.
不幸的是,Java中的数组定义以某种方式打破。 的String []
被认为是对象[]
的一个亚型,但那是的错误的!如需了解协变和逆变更详细的解释,但这个本质:A型应该算是另一种类型的子类型只有在满足亚型的的超类型的所有义务的。这意味着,如果你得到一个子类对象,而不是一个超对象,你不应该期望的行为矛盾超类型的合同。
Unfortunately, the array definition in Java is somehow broken. String[]
is considered a subtype of Object[]
, but that is wrong! For a more detailed explanation read about "covariance and contravariance", but the essence it this: A type should be considered a subtype of another type only if the subtype fulfills all obligations of the supertype. That means, that if you get a subtype object instead of a supertype object, you should not expect behavior contradictory to supertype contract.
问题是,的String []
只支持一个部分的对象[]
合同。例如,您可以的阅读的对象
从对象[]
值。而且你还可以阅读的对象
值(这恰好是字符串
对象)从的String []
。到现在为止还挺好。问题是与合同其他部分。你可以把的任何的对象
到对象[]
。但你不能把的任何的对象
到的String []
。因此,的String []
不应该被视为的子类型对象[]
,但Java规范说,这是。因此,我们产生的后果是这样的。
Problem is that String[]
only supports a part of Object[]
contract. For example you can read Object
values from Object[]
. And you can also read Object
values (which happen to be String
objects) from String[]
. So far so good. Problem is with the other part of contract. You can put any Object
into Object[]
. But you cannot put any Object
into String[]
. Therefore, String[]
should not be considered a subtype of Object[]
, but Java specification says it is. And thus we have consequences like this.
(注意,类似的情况在泛型类又出现了,不过这次它解决了的正确的列表&LT;字符串&GT;
是不的列表℃的亚型;对象&gt;
;如果你想拥有这些共同的超类型,则需要列表&LT; ?&GT;
,这是只读的这是应该的也是使用数组;。但它不是又因为向后兼容性,为时已晚去改变它)
(Note that a similar situation appeared again with the generic classes, but this time it was solved correctly. List<String>
is not a subtype of List<Object>
; and if you want to have a common supertype for these, you need List<?>
, which is read-only. This is how it should be also with arrays; but it's not. And because of the backwards compatibility, it is too late to change it.)
在你的第一个例子中的 String.split
函数创建一个的String []
对象。你可以把它变成一个对象[]
变量,但对象遗体的String []
。这就是为什么它拒绝了整数
值。你必须创建一个新的对象[]
阵列,并复制值。您可以使用 System.arraycopy
的功能来复制数据,但你不能避免创建新的数组。
In your first example the String.split
function creates a String[]
object. You can put it into a Object[]
variable, but the object remains String[]
. This is why it rejects an Integer
value. You have to create a new Objects[]
array, and copy the values. You could use the System.arraycopy
function to copy the data, but you cannot avoid creating the new array.
这篇关于与ArrayStoreException信息处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!