为什么Java的ArrayList中每个元素的铸造,而不是每个阵列铸造使用? [英] Why does Java ArrayList use per-element casting instead of per-array casting?

查看:154
本文介绍了为什么Java的ArrayList中每个元素的铸造,而不是每个阵列铸造使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

里面会发生什么Java的的ArrayList< T> (也可能是许多其他类)是有一个内部对象[]数组=新对象[N]; ,到 T 对象写入。每当一个元素是从中读取,铸造收益率(T)阵列[我]; 完成。因此,在每一个读一投。

What happens inside Java's ArrayList<T> (and probably many other classes) is that there is an internal Object[] array = new Object[n];, to which T Objects are written. Whenever an element is read from it, a cast return (T) array[i]; is done. So, a cast on every single read.

我不知道为什么这样做。对我来说,好像他们只是在做不必要的转换。那岂不是更符合逻辑,也略快于只创建一个 T []数组=(T [])新对象[N]; ,然后就返回的数组[我]; 不投?这是每个阵列的创作,这是通常只有一个铸铁的的小于读的数量。

I wonder why this is done. To me, it seems like they're just doing unnecessary casts. Wouldn't it be more logical and also slightly faster to just create a T[] array = (T[]) new Object[n]; and then just return array[i]; without cast? This is only one cast per array creation, which is usually far less than the number of reads.

为什么他们的方法是preferred?我不明白为什么我的想法是不严格把好?

Why is their method to be preferred? I fail to see why my idea isn't strictly better?

推荐答案

这是比这更复杂:仿制药被删除字节code和的擦除 T [] 对象[] 。同样, GET的返回值()变成对象。要保留类型系统,上课的时候​​实际上使用了一个检查转换插,诚信即

It's more complicated than that: generics are erased in byte code, and the erasure of T[] is Object[]. Likewise, the return value of get() becomes Object. To retain integrity of the type system, a checked cast is inserted when the class is actually used, i.e.

Integer i = list.get(0);

将被删除,以

Integer i = (Integer) list.get(0);

既然如此,内ArrayList的任何类型的检查是多余的。但它确实跑题了,因为这两个(T)(T [])选中的广播,以及承担任何运行时开销。

That being the case, any type check within ArrayList is redundant. But it's really beside the point, because both (T) and (T[]) are unchecked casts, and incur no runtime overhead.

,可以写一个检查的ArrayList,做:

One could write a checked ArrayList that does:

T[] array = Array.newInstance(tClass, n);

这将prevent的堆污染的,但在一个冗余类型检查的价格(你不能在PSS调用code中的synthentic投燮$ P $)。这还需要调用者提供的ArrayList的元素类型的类对象,其杂波API和使其难以在一般的code使用。

This would prevent heap pollution, but at the price of a redundant type check (you can not suppress the synthentic cast in calling code). It would also require the caller to provide the ArrayList with the class object of the element type, which clutters its api and makes it harder to use in generic code.

编辑:为什么通用阵列创建禁止

一个问题是阵列进行检查,而仿制药是听之任之。这就是:

One problem is that arrays are checked, while generics are unchecked. That is:

Object[] array = new String[1];
array[0] = 1; // throws ArrayStoreException

ArrayList list = new ArrayList<String>();
list.add(1); // causes heap pollution

因此​​,组件类型的数组事项。我想这就是为什么Java语言的设计者要求我们要明确将要使用的组件类型。

Therefore, the component type of an array matters. I assume this is why the designers of the Java language require us to be explicit about which component type to use.

这篇关于为什么Java的ArrayList中每个元素的铸造,而不是每个阵列铸造使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆