Quick dump of collections, arrays


#1

Using Android Studio 3.3 Kotlin REPL

val squares = IntArray(5) { it+3 }
println(squares)

prints the type/address of the array instead of the contents.
Is there a package that can always get imported in AS, to dump out the array/collection contents instead?


#2

Please, try to check the online documentation before asking questions.

This is what you are looking for: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/content-to-string.html


#3

Just confusing because arrays should behave like lists and sets when using println().

E.g. Prints the data.

val aSet: Set<Int> = setOf(1,2,3,4,4)
println(aSet)

Why the difference? Are arrays not collections?


#4

Again, I think these are some basic Java questions you are asking.

For anything that is not a primitive (byte, int, float,…), calling toString on them returns type@hashcode by default, however all the collections in the Java Collections Framework (List, Set, Map,…) override the Object.toString() method to return its content.

I tried to explain it the best I could, also there is this SO thread you can check: https://stackoverflow.com/a/7060025/5098434


#5

Arrays are not collections.