0

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() }

1 Answer 1

2

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]
Sign up to request clarification or add additional context in comments.

2 Comments

Array<*> is the key, thank you
Just realised that only the first version is correct. The other two do not return List<String>, but List<Any?>, so I am removing them, as they do not correspond to the question asked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.