Array 包名为什么是java.lang.reflect.Array, Array的作用?

java.lang.reflect.Array类提供静态方法来动态创建和访问Java数组。

Array允许在get或set操作期间扩展转换,但如果发生缩小转换,则会抛出IllegalArgumentException异常。

此类提供了创建和操作数组的一系列静态(static)本地(native)方法。

这些功能即使不通过此类,也可以实现,比如创建数组。

Object arr = Array.newInstance(Integer.class , 2);//使用Array类创建数组
int [] a = new int [2];//不适用Array方法也可以实现创建数组的功能

另外给元素赋值和获取元素值也是这种情况。
那么Array类存在的意义是什么呢?或者说上面两种实现方式有什么不同呢?

为什么会在java.lang.reflect包中

一目了然啊,因为需要动态创建数组的时候,这样只能用反射API啊,

所以此类就有意义啦,所以在reflect中。

翻译

This class is quite esoteric - most uses of arrays know the type of the array, so this class is typically most useful when implementing code that handles arrays generically.

这个类非常深奥 - 大多数数组的使用都知道数组的类型,因此在实现一般处理数组的代码时,这个类通常最有用。

There is no array superclass for all arrays, so there is no uniform way of accessing elements or the size of an array regardless of type. The java.lang.reflect.Array fills this gap and allows you to access the array in the same way regardless from type. For example, to get the value at a given index from any array (returned as an object).

It's parameteric polymorphism. Sure, you could code this yourself if you know the type - you just cast. If you don't know the array type, or it can be several types, you would check the possibilities and cast appropriately - which is what the code in reflect.Array does.

所有数组都没有数组超类,因此无论类型如何,都没有统一的方法来访问元素或数组的大小。 java.lang.reflect.Array填补了这个空白,允许您以相同的方式访问数组,而不管类型如何。 例如,从任何数组获取给定索引处的值(作为对象返回)。

EDIT: In response to the comment. Consider how you would solve this problem - how to count the number of times a value is duplicated in an array. Without the type-agnostic Array class, this would not be possible to code, without explicitly casting the array, so you would need a different function for each array type. Here, we have one function that handles any type of array.

EDIT: 回应评论。 考虑如何解决此问题 - 如何计算值在数组中重复的次数。 如果没有类型无关的Array类,如果没有显式地转换数组,就不可能进行编码,因此每个数组类型都需要不同的函数。 在这里,我们有一个处理任何类型数组的函数。

public Map<Object, Integer> countDuplicates(Object anArray)
{
    if (!anArray.getClass().isArray())
        throw new IllegalArgumentException("anArray is not an array");

    Map<Object,Integer> dedup = new HashMap<Object,Integer>();
    int length = Array.getLength(anArray);
    for (int i=0; i<length; i++)
    {
        Object value = Array.get(anArray, i);         
        Integer count = dedup.get(value);
        dedup.put(value, count==null ? 1 : count+1);
    }
    return dedup;
}

EDIT2: Regarding the get*() and set*() methods. The source code link above links to Apache harmony. The implementation there does not adhere to the Sun Javadocs. For example, from the getInt method

EDIT2:关于get *()和set *()方法。 上面的源代码链接链接到Apache harmony。 那里的实现不遵守Sun Javadocs。 例如,来自getInt方法

@throws IllegalArgumentException If the specified object is not an array, 
or if the indexed element cannot be converted to the return type 
by an identity or widening conversion 

This implies that the actual array could be byte[]short[] or int[]. This is not the case with the Harmony implementation, which only takes an int[]. (Incidentally, the Sun implementation uses native methods for most of the Array class.) The get*() and set*() methods are there for the same reason as get()getLength() - to provide (loosely) type-agnostic array access.

Not exactly something you need to use every day, but I imagine it provides value for those that need it.

他暗示实际的数组可能是byte [],short []或int []。 Harmony实现不是这种情况,只需要一个int []。 (顺便提一下,Sun实现对大多数Array类使用本机方法。)get *()和set *()方法的原因与get(),getLength()相同 - 提供(松散)类型 -  不可知的阵列访问。

并不是每天都需要使用的东西,但我认为它为那些需要它的人提供了价值。

猜你喜欢

转载自blog.csdn.net/tianyeshiye/article/details/89372198
今日推荐