What does the * (asterisk) operator before a variable mean in Kotlin? Spread Operator
Hello! I'm learning Kotlin and came across some strange syntax that I can't understand. I encountered this code:
```kotlin
val numbers = listOf(1, 2, 3)
val moreNumbers = listOf(0, *numbers.toTypedArray(), 4, 5)
println(moreNumbers) // [0, 1, 2, 3, 4, 5]
```
What does the asterisk `*` before `numbers.toTypedArray()` mean? I know that Java has a similar operator for unpacking arrays, but in Kotlin I'm used to working with lists and arrays differently.
I tried to write similar code without the asterisk:
```kotlin
val wrong = listOf(0, numbers.toTypedArray(), 4, 5)
println(wrong) // [0, [Ljava.lang.Integer;@..., 4, 5]
```
I got some nonsense — the array object ended up in the list instead of its elements. How do I use this operator correctly? Are there alternatives? And why in Kotlin, which is considered
No answers yet.
Log in to leave an answer