Kotlin: Convert Array to Java varargs

2019-01-15 12:07发布

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)

How to pass an ArrayList to a varargs method parameter?

1条回答
欢心
2楼-- · 2019-01-15 12:30

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

查看更多
登录 后发表回答