我有java包 有Host类和Client类可以接收多个主机
public final class Host {
public final String name;
public final int port;
}
public class SomeClient{...
public Client(Host... hosts) throws Exception {
....
}
我正在编写 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 ???)
}
如何将 scala 字符串数组映射并转换为 Host[],以便正确创建客户端?
最佳答案
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:_*)
}
您可以检查 repeated parameters 的 scala 引用部分4.6.2
关于scala - 如何将scala数组转换为Java数组[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33251027/