列表
列表的初始化及對其首尾的訪問:
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)
數組
創建長度已知但內容未知的數組:
scala> val fiveInts = new Array[Int](5) fiveInts: Array[Int] = Array(0, 0, 0, 0, 0)
根據已知元素初始化數組:
scala> val fiveToOne = Array(5, 4, 3, 2, 1)
fiveToOne: Array[Int] = Array(5, 4, 3, 2, 1)
訪問和更新數組元素:
scala> fiveInts(0) = fiveToOne(4) scala> fiveInts res18: Array[Int] = Array(1, 0, 0, 0, 0)
列表緩存
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)
數組緩存
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