如何将kotlin系列作为varagrs传递? [英] How to pass a kotlin collection as varagrs?
本文介绍了如何将kotlin系列作为varagrs传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
乍一看,只需要将集合转换为数组并将其传递给方法,但这不起作用:
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
没有解决方法???
推荐答案
Array
可以通过在 *
前加上 *
来作为 vararg
参数传递给它:
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.
List
的实例可以转换为 vararg
如下:
An instance of List
can be converted to vararg
as follows:
val listAsArr = listOf("a", "b").toTypedArray()
Paths.get("", * listAsArr)
这篇关于如何将kotlin系列作为varagrs传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文