gpt4 book ai didi

java - 在Java中,如何动态判断数组的类型?

转载 作者:太空宇宙 更新时间:2023-11-04 11:21:22 26 4
gpt4 key购买 nike

Object o = new Long[0]
System.out.println( o.getClass().isArray() )
System.out.println( o.getClass().getName() )
Class ofArray = ???

运行前 3 行会发出信号;

true
[Ljava.lang.Long;

我怎样才能得到???打字长吗?我可以解析字符串并执行 Class.forname(),但这很糟糕。有什么简单的方法吗?

最佳答案

直接写

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

来自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.

关于java - 在Java中,如何动态判断数组的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44862996/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com