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

标签 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 {
  ....
}

我正在编写 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/

相关文章:

scala - Play 框架堆栈跟踪日志记录和日志记录配置

scala - 引用类型参数的抽象类型成员

scala - 使用 Scala 将字符串转换为 Spark 的时间戳

scala - 以编程方式创建 Akka Dispatcher

scala - CPS 库,用于使用 Scala 2.10 futures 实现良好的数据流

c - 使用 Scala 构建类 C 的解释器

scala - Scala 中的无点风格案例

scala - Spark 任务不可序列化(案例类)

scala - 如何在spark/scala中对数据帧的一列的值求和

javascript - Scala.js:对象文字,键名中有冒号