Convert Kotlin Array to Java varargs

JavaKotlinInteropKotlin Interop

Java Problem Overview


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)

https://stackoverflow.com/questions/9863742/how-to-pass-an-arraylist-to-a-varargs-method-parameter

Java Solutions


Solution 1 - Java

There’s the spread operator which is denoted by *.
The spread operator is placed in front of 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 *):

Please note that the spread operator is only defined for arrays, and cannot be used on a list directly. When dealing with a list, use e.g.toTypedArray() to transform it to an array:

 *list.toTypedArray()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionrobie2011View Question on Stackoverflow
Solution 1 - Javas1m0nw1View Answer on Stackoverflow