scala編程第17章學習筆記(1)——集合類型

列表es6

列表的初始化及對其首尾的訪問:數組

scala> val colors = List("red", "blue", "green")
colors: List[String] = List(red, blue, green)

scala> colors.head
res15: String = red

scala> colors.tail
res16: List[String] = List(blue, green)

數組緩存

建立長度已知但內容未知的數組:es5

scala> val fiveInts = new Array[Int](5)
fiveInts: Array[Int] = Array(0, 0, 0, 0, 0)

根據已知元素初始化數組:spa

scala> val fiveToOne = Array(5, 4, 3, 2, 1)
fiveToOne: Array[Int] = Array(5, 4, 3, 2, 1)

訪問和更新數組元素:scala

scala> fiveInts(0) = fiveToOne(4)

scala> fiveInts
res18: Array[Int] = Array(1, 0, 0, 0, 0)

列表緩存code

ListBuffer是可變對象(包含在scala.collection.mutable包中),它能夠更高效地經過添加元素的方式構建列表。ListBuffer可以支持常量時間的添加和前綴操做。元素的添加使用+=操做符,前綴使用+:操做符。完成以後,能夠經過ListBuffer調用toList方法得到List。舉例以下:對象

scala> val buf = new ListBuffer[Int]
buf: scala.collection.mutable.ListBuffer[Int] = ListBuffer()

scala> buf += 1
res20: buf.type = ListBuffer(1)

scala> buf += 2
res21: buf.type = ListBuffer(1, 2)

scala> buf
res22: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2)

scala> 3 +: buf
res23: scala.collection.mutable.ListBuffer[Int] = ListBuffer(3, 1, 2)

scala> buf.toList
res24: List[Int] = List(1, 2)

數組緩存blog

ArrayBuffer與數組相似,只是額外還容許你在序列的開始或結束的地方添加和刪除元素。全部的Array操做都被保留,只是因爲實現中的包裝層致使執行的稍微有些慢。索引

建立ArrayBuffer的時候,你必須指定它的類型參數,但能夠不指定長度。ArrayBuffer能夠自動調整分配的空間:

ArrayBuffer還能用+=操做添加元素:

scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer

scala> val buf = new ArrayBuffer[Int]()
buf: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()

scala> buf += 12
res27: buf.type = ArrayBuffer(12)

scala> buf += 15
res29: buf.type = ArrayBuffer(12, 15)

全部數組能使用的方法數組緩存都能用。如能夠得到ArrayBuffer的長度,或經過索引訪問元素:

scala> buf.length
res30: Int = 2

scala> buf(0)
res31: Int = 12

隊列
Scala的集合庫提供了可變和不可變的Queue。如下方法建立空的不可變隊列:

scala> import scala.collection.immutable.Queue
import scala.collection.immutable.Queue

scala> val empty = Queue[Int]()
empty: scala.collection.immutable.Queue[Int] = Queue()

可使用enqueue爲不可變隊列添加元素:

scala> val has1 = empty.enqueue(1)
has1: scala.collection.immutable.Queue[Int] = Queue(1)

若是要添加多個元素的話,能夠把集合看成enqueue調用的參數:

scala> val has123 = has1.enqueue(List(2, 3))
has123: scala.collection.immutable.Queue[Int] = Queue(1, 2, 3)

從隊列的頭部移除元素,可使用dequeue:

scala> val (element, has23) = has123.dequeue
element: Int = 1
has23: scala.collection.immutable.Queue[Int] = Queue(2, 3)

對於不可變隊列來講,dequeue方法將返回由隊列頭部元素和移除該元素以後的剩餘隊列組成的對偶(Tuple2)。
可變隊列的使用方式與不可變隊列同樣,只是代之以enqueue方法,可使用+=及++=操做符添加元素。還有,對於可變隊列來講,dequeue方法將只從隊列移除頭元素並返回。舉例以下:

scala> import scala.collection.mutable.Queue
import scala.collection.mutable.Queue

scala> val queue = Queue[String]()
queue: scala.collection.mutable.Queue[String] = Queue()

scala> queue += "a"
res0: queue.type = Queue(a)

scala> queue ++= List("b", "c")
res1: queue.type = Queue(a, b, c)

scala> queue
res2: scala.collection.mutable.Queue[String] = Queue(a, b, c)

scala> queue.dequeue
res3: String = a

scala> queue
res4: scala.collection.mutable.Queue[String] = Queue(b, c)

Stack一樣在Scala的集合庫中有可變和不可變兩個版本。元素的推入使用push,彈出使用pop,只獲取棧頂的元素而不移除可使用top。下面是使用可變棧的例子:

scala> import scala.collection.mutable.Stack
import scala.collection.mutable.Stack

scala> val stack = new Stack[Int]
stack: scala.collection.mutable.Stack[Int] = Stack()

scala> stack.push(1)
res5: stack.type = Stack(1)

scala> stack
res6: scala.collection.mutable.Stack[Int] = Stack(1)

scala> stack.push(2)
res7: stack.type = Stack(2, 1)

scala> stack
res8: scala.collection.mutable.Stack[Int] = Stack(2, 1)

scala> stack.top
res9: Int = 2

scala> stack
res10: scala.collection.mutable.Stack[Int] = Stack(2, 1)

scala> stack.pop
res11: Int = 2

scala> stack
res12: scala.collection.mutable.Stack[Int] = Stack(1)

字符串(經RichString隱式轉換)

RichString也是應該知道的序列,它的類型是Seq[Char]。由於Predef包含了從String到RichString的隱式轉換,因此你能夠把任何字符串看成Seq[Char]。舉例以下:

scala> def hasUpperCase(s:String) = s.exists(_.isUpper)
hasUpperCase: (s: String)Boolean

scala> hasUpperCase("Robert Frost")
res13: Boolean = true

scala> hasUpperCase("e e cummings")
res14: Boolean = false
相關文章
相關標籤/搜索