将Kotlin数组转换为Java varargs [英] Convert Kotlin Array to Java varargs
本文介绍了将Kotlin数组转换为Java varargs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将Kotlin Array
转换为varargs Java String[]
?
How can I convert my Kotlin Array
to a varargs Java String[]
?
val angularRoutings =
arrayOf<String>("/language", "/home")
// this doesn't work
web.ignoring().antMatchers(angularRoutings)
推荐答案
您应使用"传播算子",它看起来像这样:*
传播算子必须是前缀为数组参数:
You should use the "spread operator", which looks like this: *
The spread operator needs to be prefixed to the array argument:
antMatchers(*angularRoutings)
有关更多信息,请参见文档:
For further information, see the documentation:
当我们调用
vararg
函数时,我们可以一个一个地传递参数,例如asList(1, 2, 3)
,或者,如果我们已经有一个数组并将其内容传递给该函数,则可以使用传播运算符(使用*
前缀该数组):
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*
):
这篇关于将Kotlin数组转换为Java varargs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文