Home > Mobile >  Kotlin: Convert String[] to Any to ArrayList<String>
Kotlin: Convert String[] to Any to ArrayList<String>

Time:11-24

Need to convert String[] and pass it on a parameter that has Any data type, then from Any need to convert it to ArrayList<String>. Tried this but not working as it returns memory address.

arrayOf(anyObject).map { it.toString() }

CodePudding user response:

You need to cast anyObject to Array, not create a new Array with anyObject as Element:

val str = arrayOf("a", "b", "c")
val anyObject: Any = str
val result = (anyObject as Array<*>).map { it.toString() }

println(result)   // [a, b, c]
  • Related