Android 如何检查变量是否是Kotlin中的数组

Android 如何检查变量是否是Kotlin中的数组,android,kotlin,Android,Kotlin,我的Kotlin代码是 这是一个获取相机特性的函数 如何正确检查变量是否为数组?t is array对于对象数组(array)为true,但对于基本数组(IntArray等)为false。所以你可能想要 holder.keyValue.text = when(val t = cameraController.getCharacteristicInfo(myDataset[position])) { is Array<*> -> Arrays.toString(t)

我的Kotlin代码是

这是一个获取相机特性的函数

如何正确检查变量是否为数组?

t is array
对于对象数组(
array
)为true,但对于基本数组(
IntArray
等)为false。所以你可能想要

holder.keyValue.text = when(val t = cameraController.getCharacteristicInfo(myDataset[position])) {
    is Array<*> -> Arrays.toString(t)
    is IntArray -> Arrays.toString(t)
    ...
    else -> t.toString()
}

即使智能强制转换在这种情况下可用(它们不可用)。

面临同样的问题,并使用
类的
isArray

>>> arrayOf("a","b","c")::class.java.isArray
res1: kotlin.Boolean = true
>>> IntArray(1)::class.java.isArray
res2: kotlin.Boolean = true
>>> Array<String>(1) { "a" }::class.java.isArray
res3: kotlin.Boolean = true
>>> Any::class.java.isArray
res4: kotlin.Boolean = false
数组(“a”、“b”、“c”)::class.java.isArray res1:kotlin.Boolean=true >>>IntArray(1)::class.java.isArray res2:kotlin.Boolean=true >>>数组(1){“a”}::class.java.isArray res3:kotlin.Boolean=true >>>Any::class.java.isArray res4:kotlin.Boolean=false

注意:如果您的目标不是JVM,这可能不可用。

您可以向我们展示
getCharacteristicInfo()
的代码吗?好的。补充说。这是一个获取相机特性的函数。它似乎不是真正的数组类型。您是否可以编辑代码以显示
特征的类型
以及
myDataset[position]
变量?是否可以显示
myDataset[position]
变量的类型
getCharacteristicInfo
返回与作为参数传递的
键相关的
T
类型的值
myDataset[position]
是某种
(实际上,代码是编译的),但也可能是-例如-
,这迫使
t
的类型为
字符串
。。。
holder.keyValue.text = when(val t = cameraController.getCharacteristicInfo(myDataset[position])) {
    is Array<*> -> Arrays.toString(t)
    is IntArray -> Arrays.toString(t)
    ...
    else -> t.toString()
}
is Array<*>, is IntArray, ... -> Arrays.toString(t)
>>> arrayOf("a","b","c")::class.java.isArray
res1: kotlin.Boolean = true
>>> IntArray(1)::class.java.isArray
res2: kotlin.Boolean = true
>>> Array<String>(1) { "a" }::class.java.isArray
res3: kotlin.Boolean = true
>>> Any::class.java.isArray
res4: kotlin.Boolean = false