简体   繁体   中英

Scala implicit conversion to a Java array not working

I'm pretty new to Scala and I've hit my first hurdle ...

java.nio.file.Paths has this method:

public static Path get(String first, String ... more)

This java code compiles (and runs):

String[] tokens = new String[] { "/home", "toby", "mydir" };
Path path = Paths.get(tokens[0], Arrays.copyOfRange(tokens, 1, tokens.length -1));

However this scala code does not compile:

import collection.JavaConversions._
...
val tokens = Array("/home", "toby", "mydir")
val path = Paths.get(tokens(0), tokens.tail)

The error I get is "type mismatch; found : Array[String] required: String"

What am I missing? Thanks

Paths.get does not want an array as the second parameter, String... more is the varargs notation.

Try:

val path = Paths.get(tokens.head, tokens.tail: _*)
// path: java.nio.file.Path = /home/toby/mydir

Look at this question for more explanation on _* .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM