代码之家  ›  专栏  ›  技术社区  ›  Basilevs

如何从Scala 2.7.7中的Iterable创建数组?

  •  1
  • Basilevs  · 技术社区  · 14 年前

    我在用 斯卡拉2.7.7

    我在访问文档时遇到了困难,因此代码片段会更大。

    上下文

    我看到了两种解决问题的方法

    • 使用一个16字节长的数组,从Iterable中过滤它并返回它的一部分,如果不是所有的字段都被使用(是否有函数在2.7.7中填充数组?怎么得到这个角色。
    • 使用动态长度容器并形成数组(哪个容器合适?)。

    my other question about memory leaks .

    2 回复  |  直到 8 年前
        1
  •  2
  •   user382157 user382157    14 年前

    在Scala 2.7中, Iterable 有一个名为 copyToArray .

        2
  •  1
  •   Kevin Wright    14 年前

    我强烈建议您不要在这里使用数组,除非您必须使用特定的库/框架,然后需要一个数组。

    通常,最好使用本机Scala类型:

    String.split(separator).map(_.toByte).toList
    //or
    String.split(separator).map(_.toByte).toSeq
    

    更新

    假设原始字符串是主机名的分隔列表,为什么不:

    val namesStr = "www.sun.com;www.stackoverflow.com;www.scala-tools.com"
    val separator = ";"
    val addresses = namesStr.split(separator).map(InetAddress.getByName)
    

    这会给你 InetAddress 实例。

    推荐文章