0

I have working with kotlin for more than one year and written a lot of code so far. However today i saw following code, and honestly i do not understand the syntax. A lot of research resulted in 0 output. The code i am looks like this

var list1 = arrayOfNulls<Int>(7)
var list2 = arrayOfNulls<Int>(8)
arrayListOf(*list2) + arrayListOf(*list1)

The syntax is new for me and i have not seen it before. Can someone explain it for me. What i do not understans ius the star part (*) in the arrayListOf(*list2)

  • 2
    Does this answer your question? https://stackoverflow.com/questions/39389003/kotlin-asterisk-operator-before-variable-name-or-spread-operator-in-kotlin – k314159 Aug 19 '21 at 08:55

1 Answers1

3

Explination from the official Kotlin Doc:

When you call a vararg -function, you can pass arguments individually, for example asList(1, 2, 3). If you already have an array and want to pass its contents to the function, use the spread operator (prefix the array with *):

val a = arrayOf(1, 2, 3)
val list = asList(-1, 0, *a, 4)

Kotlin Docs - varargs

Tobse
  • 1,176
  • 1
  • 9
  • 22