Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

281
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda