在Java中,如何动态确定数组的类型?

时间:2022-08-11 07:21:03
Object o = new Long[0]
System.out.println( o.getClass().isArray() )
System.out.println( o.getClass().getName() )
Class ofArray = ???

Running the first 3 lines emits;

运行前3行发出;

true
[Ljava.lang.Long;

How do I get ??? to be type long? I could parse the string and do a Class.forname(), but thats grotty. What's the easy way?

如何得到 ???打字长?我可以解析字符串并执行Class.forname(),但那很糟糕。什么是简单的方法?

3 个解决方案

#1


61  

Just write

Class ofArray = o.getClass().getComponentType();

From the JavaDoc:

来自JavaDoc:

public Class<?> getComponentType()

public Class getComponentType()

Returns the Class representing the component type of an array. If this class does not represent an array class this method returns null.

返回表示数组的组件类型的Class。如果此类不表示数组类,则此方法返回null。

#2


21  

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#getComponentType():

public Class<?> getComponentType()

Returns the Class representing the component type of an array. If this class does not represent an array class this method returns null...

返回表示数组的组件类型的Class。如果此类不表示数组类,则此方法返回null ...

#3


6  

@ddimitrov is the correct answer. Put into code it looks like this:

@ddimitrov是正确的答案。放入代码看起来像这样:

public <T> Class<T> testArray(T[] array) {
    return array.getClass().getComponentType();
}

Even more generally, we can test first to see if the type represents an array, and then get its component:

更一般地说,我们可以先测试一下这个类型是否代表一个数组,然后得到它的组件:

Object maybeArray = ...
Class<?> clazz = maybeArray.getClass();
if (clazz.isArray()) {
    System.out.printf("Array of type %s", clazz.getComponentType());
} else {
    System.out.println("Not an array");
}

A specific example would be applying this method to an array for which the component type is already known:

一个具体的例子是将此方法应用于已知组件类型的数组:

String[] arr = {"Daniel", "Chris", "Joseph"};
arr.getClass().getComponentType();              // => java.lang.String

Pretty straightforward!

#1


61  

Just write

Class ofArray = o.getClass().getComponentType();

From the JavaDoc:

来自JavaDoc:

public Class<?> getComponentType()

public Class getComponentType()

Returns the Class representing the component type of an array. If this class does not represent an array class this method returns null.

返回表示数组的组件类型的Class。如果此类不表示数组类,则此方法返回null。

#2


21  

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#getComponentType():

public Class<?> getComponentType()

Returns the Class representing the component type of an array. If this class does not represent an array class this method returns null...

返回表示数组的组件类型的Class。如果此类不表示数组类,则此方法返回null ...

#3


6  

@ddimitrov is the correct answer. Put into code it looks like this:

@ddimitrov是正确的答案。放入代码看起来像这样:

public <T> Class<T> testArray(T[] array) {
    return array.getClass().getComponentType();
}

Even more generally, we can test first to see if the type represents an array, and then get its component:

更一般地说,我们可以先测试一下这个类型是否代表一个数组,然后得到它的组件:

Object maybeArray = ...
Class<?> clazz = maybeArray.getClass();
if (clazz.isArray()) {
    System.out.printf("Array of type %s", clazz.getComponentType());
} else {
    System.out.println("Not an array");
}

A specific example would be applying this method to an array for which the component type is already known:

一个具体的例子是将此方法应用于已知组件类型的数组:

String[] arr = {"Daniel", "Chris", "Joseph"};
arr.getClass().getComponentType();              // => java.lang.String

Pretty straightforward!