zoukankan      html  css  js  c++  java
  • Scala List

    1 介绍

    Scala中列表List类似于数组,List所有元素都具有相同的类型,但有两个重要的区别。

    • 首先,列表是不可变的,这意味着一个列表的元素可以不被分配来改变。
    • 第二,列表表示一个链表,而数组平坦的。

    具有T类型的元素的列表的类型被写为List[T]。例如,这里有各种数据类型定义的一些列表:

    // List of Strings
    val fruit: List[String] = List("apples", "oranges", "pears")
    
    // List of Integers
    val nums: List[Int] = List(1, 2, 3, 4)
    
    // Empty List.
    val empty: List[Nothing] = List()
    
    // Two dimensional list
    val dim: List[List[Int]] =
       List(
          List(1, 0, 0),
          List(0, 1, 0),
          List(0, 0, 1)
       )
    

      

    List是不可变的,操作List返回的是新的List对象.

    val l = List(1,2,3,4,5)

    List中元素类型是一致,上面的l类型就是List[Int]

    2 创建List

       (1) Lisp-style and Java-style

    scala> val list = 1 :: 2 :: 3 :: Nil
    list: List[Int] = List(1, 2, 3)
    
    scala> val list = List(1,2,3)
    x: List[Int] = List(1, 2, 3)

        (2) range函数。

    scala> val x = List.range(1,10)
    x: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9)
    scala> val x = List.range(0,10,2)
    x: List[Int] = List(0, 2, 4, 6, 8)
    //或者直接使用 toList
    val list3 = 10 to 20 toList 
    list3: List[Int] = List(10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
    

      

       (3) fill函数

    scala> val x = List.fill(3)("foo")
    x: List[java.lang.String] = List(foo, foo, foo)
    

      (4) tabulate

           List.tabulate() 方法是通过给定的函数来创建列表。
    方法的第一个参数为元素的数量,可以是二维的,第二个参数为指定的函数,我们通过指定的函数计算结果并返回值插入到列表中,起始值为 0,实例如下:

    object Test {
       def main(args: Array[String]) {
          // Creates 5 elements using the given function.
          val squares = List.tabulate(6)(n => n * n)
          println( "squares : " + squares  )
    
          // 
          val mul = List.tabulate( 4,5 )( _ * _ )      
          println( "mul : " + mul  )
       }
    }
    

      输出结果

    C:/>scalac Test.scala
    C:/>scala Test
    squares : List(0, 1, 4, 9, 16, 25)
    mul : List(List(0, 0, 0, 0, 0), List(0, 1, 2, 3, 4), 
               List(0, 2, 4, 6, 8), List(0, 3, 6, 9, 12))
    

    3 增加元素

    val left = List(1,2,3)
    val right = List(4,5,6)
    
    //合并两个List
    left ++ right   // List(1,2,3,4,5,6)
    left:::right    // List(1,2,3,4,5,6)
    right:::left    // List(4,5,6,1,2,3)
    right.:::(left)  // List(1,2,3,4,5,6)
    left.:::(right)  // List(4,5,6,1,2,3)
    
    //在List头部添加元素
    0::left      //List(0,1,2,3)
    left.::(0)     //List(0,1,2,3)
    0+:left    //List(0,1,2,3)
    
    //在List尾部添加元素
    left :+ 4    //List(1,2,3,4)
    

      任何以冒号结果的操作符,都是右绑定的,即 0 :: List(1,2,3) = List(1,2,3).::(0) = List(0,1,2,3) 从这里可以看出操作::其实是右边List的操作符,而非左边Int类型的操作符

     4 迭代List

    scala> val x = List(1,2,3)
    x: List[Int] = List(1, 2, 3)
    
    scala> x.foreach { println }
    1
    2
    3
    
    scala> var sum = 0
    sum: Int = 0
    
    scala> val x = List(1,2,3)
    x: List[Int] = List(1, 2, 3)
    
    scala> x.foreach(sum += _)
    
    scala> println(sum)
    6
    
    scala> val names = List("Bob", "Fred", "Joe", "Julia", "Kim")
    names: List[java.lang.String] = List(Bob, Fred, Joe, Julia, Kim)
    
    scala> for (name <- names if name.startsWith("J"))
         | println(name)
    Joe
    Julia
    
    scala> val x = List(1,2,3,4,5,6,7,8,9,10)
    x: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    
    // create a list of all the even numbers in the list
    scala> val evens = x.filter(a => a % 2 == 0)
    evens: List[Int] = List(2, 4, 6, 8, 10)
    

      

    5 map函数

    The Scala List map function "transforms each element of a collection based on a function."

    scala> val x = List(1,2,3)
    x: List[Int] = List(1, 2, 3)
    
    scala> val y = x.map(a => a * 2)
    y: List[Int] = List(2, 4, 6)
    scala> val y = x.map(_ * 2)
    y: List[Int] = List(2, 4, 6)

      更具体的map、flattenMap等见http://www.cnblogs.com/yxzfscg/p/4997182.html

    http://my.oschina.net/brucegao/blog/372362

    http://www.yiibai.com/scala/scala_lists.html

    http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.List

    http://alvinalexander.com/scala/scala-list-class-examples

    http://www.oschina.net/code/snippet_226405_19569

  • 相关阅读:
    cocos2d-x quick 学习 二 Hello world
    cocos2d-x quick 学习 一 环境
    给自己
    最近遇到问题
    lua 基础 1
    lua 学习 (一 )Mac搭建lua环境和ide
    VueDay1
    Git的简单使用
    web常用第三方接口
    2.node基础知识笔记
  • 原文地址:https://www.cnblogs.com/yxzfscg/p/5012358.html
Copyright © 2011-2022 走看看