Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

274
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!