How to pass a kotlin collection as varagrs?

女生的网名这么多〃 提交于 2019-12-19 16:59:12

问题


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").toTypedArray()
Paths.get("", toTypedArray) // <- compilation error here

No workarounds???


回答1:


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 question here.

An instance of List can be converted to varargas follows:

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


来源:https://stackoverflow.com/questions/46418550/how-to-pass-a-kotlin-collection-as-varagrs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!