300

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?

0

2 Answers 2

586

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()
Sign up to request clarification or add additional context in comments.

6 Comments

And how to convert back?
@Iacas What do you mean by "convert back"? A vararg expects individual elements, and *array is the Kotlin way to say "treat this array as individual elements for that purpose". Within the vararg-function the vararg parameter will be an array anyway. To convert individual elements to an array you can use arrayOf(...), but you don't need that in this case.
Note that this has a very high-performance penalty. sites.google.com/a/athaydes.com/renato-athaydes/posts/… check Varargs, or medium.com/@BladeCoder/…
Is there any way to avoid using spread operator and pass some array or list to a method that accepts vararg?
@DarioSeidl He means: What if we have varargs of string, and we wish to pass to a function that expects an array of strings?
|
0

First of all, I want to say that @s1m0nw1's answer is awesome. Kotlin's spread operator (or *) lets you decompose an array into a list of individual elements when passing it to a function. However, I'd like to show you how to convert Kotlin's Array to Java's Varargs without spread operator. Here's the code:

fun toListFrom(vararg severalStrs: String): List<String> {
    return severalStrs.asList()
}

fun main() {
    val elements = arrayOf("He","l","lo"," ","Kot","li","n!")
    val concatenation = toListFrom(elements.reduce { a, b -> a + b })
    val result = concatenation.first()

    println(result)                          // Hello Kotlin!
}

And this is what my code will look like if I use * spread operator.

fun concatenate(vararg severalStrs: String): String {
    return severalStrs.reduce { a, b -> a + b }
}

fun main() {
    val elements = arrayOf("He","l","lo"," ","Kot","li","n!")
    val concat = concatenate(*elements)

    println(concat)                          // Hello Kotlin!
}

Tested on Kotlin 2.0.0 Playground.

1 Comment

As far as I can see - this answer reduces the individual elements to a single String and passes a varargs (of one arg) to the function. That seems different to passing in a varargs of the individual elements (which is what happens with the spread operator). So I wonder if the answer needs more clarification?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.