In Java, how do I dynamically determine the type of an array?

后端 未结 3 1388
醉梦人生
醉梦人生 2020-12-06 04:26
Object o = new Long[0]
System.out.println( o.getClass().isArray() )
System.out.println( o.getClass().getName() )
Class ofArray = ???

Running the fi

3条回答
  •  不知归路
    2020-12-06 04:48

    Just write

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

    From the JavaDoc:

    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.

提交回复
热议问题