Kotlin asterisk operator before variable name or Spread Operator in Kotlin

后端 未结 5 716
猫巷女王i
猫巷女王i 2020-11-30 00:59

I want to know what exactly an asterisk does before a variable name in Kotlin. I saw this (*args) in a Spring boot Kotlin example:

@SpringBootApp         


        
5条回答
  •  死守一世寂寞
    2020-11-30 01:28

    As described in the documentation this is a spread operator:

    When we call a vararg-function, we can pass arguments one-by-one, e.g. asList(1, 2, 3), or, if we already have an array and want to pass its contents to the function, we use the spread operator (prefix the array with *):

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

提交回复
热议问题