如何将scala数组转换为Java数组[]

Jul*_*ias 2 scala

我有java包有Host类和Client类可以反映许多主机

public final class Host {
   public final String name;
   public final int port; 
} 

public class SomeClient{...
   public Client(Host... hosts) throws Exception {
  ....
}
Run Code Online (Sandbox Code Playgroud)

我正在编写scala代码来创建这个客户端

//  the hosts example is hosts1,host2,host3
def getClient(hosts:String) :SomeClient ={
   val default_port:Int = 100
   val hostsArr: Array[String] =hosts.split(",")
   new Client (hostArr ???)
 }
Run Code Online (Sandbox Code Playgroud)

如何将scala数组字符串映射并转换为Host [],以便正确创建客户端?

Fat*_*mez 8

def getClient(hosts:String): SomeClient = {

    val default_port:Int = 100
    val hostsArr: Array[String] = hosts.split(",")

    //Here you map over array of string and create Host object for each item 
    val hosts = hostsArr map { hostStr =>
        val host = new Host()
        //Rest of assignment

        host
    }

    new Client(hosts:_*)
}
Run Code Online (Sandbox Code Playgroud)

您可以检查重复参数的scala参考部分4.6.2