Scala吧~  - 讨论区

标题:Scala Tutorial 006:List容器使用方法

2014年02月19日 星期三 09:21

List是一种链表数据结构,其特性与Array不同,对于头部追加元素和删除元素操作,List的执行效率要比Array好很多,但对于基于index的随机访问,List的执行效率则要比Array差很多。在《Programming in Scala》中有如下论述:

Lists support fast addition and removal of items to the beginning of the list, but they do not provide fast access to arbitrary indexes because the implementation must iterate through the list linearly.

在Scala中,List有两种比较奇特的运算符,双冒号和三冒号,分别是List与元素叠加和两个List叠加的运算。

编程示例如下:

object S006_Lists {

  def main(args: Array[String]): Unit = {
    val friends=List("mengguang","mengkang");
    val otherfirends=List("menghui","laozhang");
    val allfriends=friends :::  otherfirends;
    val newfriends="xiaoli" :: allfriends ;
    for(friend <- newfriends) {
      println(friend);
    }
    val myfriends=newfriends.drop(1).dropRight(1).reverse;
    for(friend <- myfriends) {
      println("my: " + friend);
    }
  }

}

参考资料:

http://www.artima.com/pins1ed/collections.html

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2024

    京ICP备05028076号