将 Kotlin 数组转换为 Java 可变参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45854994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:12:22  来源:igfitidea点击:

Convert Kotlin Array to Java varargs

javakotlininteropkotlin-interop

提问by robie2011

How can I convert my Kotlin Arrayto a varargs Java String[]?

如何将我的 Kotlin 转换Array为可变参数 Java String[]

val angularRoutings = 
    arrayOf<String>("/language", "/home")

// this doesn't work        
web.ignoring().antMatchers(angularRoutings)

How to pass an ArrayList to a varargs method parameter?

如何将 ArrayList 传递给可变参数方法参数?

采纳答案by s1m0nw1

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:

有关更多信息,请参阅文档

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 *):

当我们调用一个vararg- 函数时,我们可以一个一个地传递参数,例如asList(1, 2, 3), 或者,如果我们已经有一个数组并且想要将其内容传递给函数,我们可以使用扩展运算符(在数组前加上*):