How to pass a kotlin collection as varagrs?

后端 未结 1 997
Happy的楠姐
Happy的楠姐 2021-01-17 21:08

At first glance it is needed just convert collection to array and pass it to method but this does not work:

val toTypedArray = Arrays.asList(\"a\", \"b\").to         


        
1条回答
  •  离开以前
    2021-01-17 21:51

    An Array can be passed as anvararg argument by prepending * to it:

    Paths.get("", *toTypedArray) 
    

    It’s called spread operator, as I already described in another answer here.

    An instance of List can be converted to vararg as follows:

    val listAsArr = 
        listOf("a", "b").toTypedArray()
    Paths.get("", *listAsArr) 
    

    0 讨论(0)
提交回复
热议问题