什么是java.lang.reflect.Array中的getter和setter方法​​的目的是什么? [英] What is the purpose of java.lang.reflect.Array's getter and setter methods?

查看:341
本文介绍了什么是java.lang.reflect.Array中的getter和setter方法​​的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java的类 java.lang.reflect中。阵列提供了一组工具,用于动态创建的阵列。然而除了它具有用于访问(获取,设置和长度)的阵列一整套方法。我不明白这一点,因为你可以(presumably会)投你的动态生成的阵列在创建一个数组,这意味着你可以使用正常的数组访问(括号标记)功能。事实上,查看源$ C ​​$ C,你可以看到这是所有的类不,铸阵,并抛出一个异常,如果转换失败。

Java Class java.lang.reflect.Array provides a set of tools for creating an array dynamically. However in addition to that it has a whole set of methods for accessing (get, set, and length) an array. I don't understand the point of this, since you can (and presumably would) cast your dynamically generated array as an array upon creation, which means you can use the normal array access (bracket notation) functionality. In fact, looking at the source code you can see that is all the class does, cast the array, and throw an exception if the cast fails.

那么,所有这些额外的方法点/用处?

So what's the point / usefulness of all of these extra methods?

更新

所有原始的得到*()设置*()方法似乎的尤其无益的,考虑到你需要知道数组的类型事先才能知道要使用的方法。

All of the primitive get*() and set*() methods seem especially unhelpful, considering you need to know the type of the array beforehand in order to know which method to use.

更新2

谢谢大家,你的投入已经很有教育意义!我实在看不出时的我想的是使用这个类比的newInstance以外的任何()(也许的getLength()),但我现在认识到这些其他的方法仍然是相当有用的。

Thanks everyone, your input has been very educational! I can't really see when I'd be using this class for anything other than newInstance() (and maybe getLength()), but I now realize these other methods are still quite useful.

推荐答案

本类是非常深奥 - 阵列的大多数用途知道数组的类型,所以执行code,处理数组时,这个类通常是最有用的一般

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.

有适用于所有的数组没有数组父类,所以访问元素或数组的大小,无论类型没有统一的方法。在 java.lang.reflect.Array中的填补了这一空白,并允许您从类型的访问,无论以同样的方式阵列。例如,为了从任何数组(返回作为一个对象)的给定索引处获得的值。

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).

这是 parameteric多态性。当然,你可以code这个自己,如果你知道类型 - 你只投。如果你不知道数组类型,也可以是多种类型,则需要检查的可能性,并适当地投 - 这是什么code在 reflect.Array 一样。

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.

编辑:在回应评论。想想你将如何解决这个问题 - 如何计算的次数值在数组中重复的次数。不带类型无关Array类,这是不可能的,以code,没有明确铸阵,所以你需要为每个数组类型不同的功能。在这里,我们有一个函数处理任何类型的数组。

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.

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:关于GET *()和set *()方法。上述链接到Apache和谐源$ C ​​$ C链接。实施有不符合太阳的Javadoc。例如,从<一href=\"http://java.sun.com/javase/6/docs/api/java/lang/reflect/Array.html#getInt%28java.lang.Object,%20int%29\"相对=nofollow>调用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 

这意味着实际的数组可能是字节[] 短[] INT [] 。这是不是与和谐的实现,只需要一个 INT [] 的情况。 (顺便说一句,Sun实现使用大多数Array类的本地方法。)的get *()和set *()方法有出于同样的原因为的get()的getLength() - 提供(宽松)类型无关的数组访问

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.

这篇关于什么是java.lang.reflect.Array中的getter和setter方法​​的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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