Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

276
Vistas
How to check if a variable is an array in Kotlin

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?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

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).

over 4 years ago · Santiago Trujillo Denunciar

0

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.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda