My Kotlin code is
val t = cameraController.getCharacteristicInfo(myDataset[position])
if (t is Array<*>) {
holder.keyValue.text = Arrays.toString(t)
} else {
holder.keyValue.text = t.toString()
}
It is not working. if (t is Array<*>) always returns false.
The code of the function getCharacteristicInfo is:
public <T> T getCharacteristicInfo(CameraCharacteristics.Key<T> key) {
return characteristics.get(key);
}
It is a function for getting camera characteristics.
How to properly check if a variable is an array?
t is Array<*> is true for object arrays (Array<Whatever>), but false for primitive arrays (IntArray etc.). So you probably want
holder.keyValue.text = when(val t = cameraController.getCharacteristicInfo(myDataset[position])) {
is Array<*> -> Arrays.toString(t)
is IntArray -> Arrays.toString(t)
...
else -> t.toString()
}
(if t is used outside elsewhere, just move the assignment outside).
Note that these are different Arrays.toString overloads, so you couldn't write
is Array<*>, is IntArray, ... -> Arrays.toString(t)
even if smart casts were available in this situation (they aren't).
Faced the same issue and used isArray of the Class:
>>> 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
NOTE: That might not be available if your target is not JVM.