- Scala集合教程
- Scala集合 - 首頁
- Scala集合 - 概述
- Scala集合 - 環境設定
- Scala集合 - 陣列
- Scala集合 - 陣列
- Scala集合 - 多維陣列
- Scala集合 - 使用範圍建立陣列
- Scala集合 - ArrayBuffer
- Scala集合 - 列表
- Scala集合 - 列表
- Scala集合 - ListBuffer
- Scala集合 - ListSet
- Scala集合 - 向量
- Scala集合 - 集合
- Scala集合 - 集合
- Scala集合 - BitSet
- Scala集合 - HashSet
- Scala集合 - TreeSet
- Scala集合 - 對映
- Scala集合 - 對映
- Scala集合 - HashMap
- Scala集合 - ListMap
- Scala集合 - 其他
- Scala集合 - 迭代器
- Scala集合 - Option
- Scala集合 - 佇列
- Scala集合 - 元組
- Scala集合 - Seq
- Scala集合 - 棧
- Scala集合 - 流
- Scala集合組合器方法
- Scala集合 - drop
- Scala集合 - dropWhile
- Scala集合 - filter
- Scala集合 - find
- Scala集合 - flatMap
- Scala集合 - flatten
- Scala集合 - fold
- Scala集合 - foldLeft
- Scala集合 - foldRight
- Scala集合 - map
- Scala集合 - partition
- Scala集合 - reduce
- Scala集合 - scan
- Scala集合 - zip
- Scala集合有用資源
- Scala集合 - 快速指南
- Scala集合 - 有用資源
- Scala集合 - 討論
Scala集合 - 列表
Scala列表與陣列非常相似,這意味著列表的所有元素都具有相同的型別,但有兩個重要的區別。首先,列表是不可變的,這意味著列表的元素不能透過賦值來更改。其次,列表表示連結串列,而陣列是扁平的。
具有型別為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)
)
所有列表都可以使用兩個基本構建塊來定義,一個尾部Nil和::,讀作cons。Nil也表示空列表。所有上述列表都可以定義如下。
// List of Strings
val fruit = "apples" :: ("oranges" :: ("pears" :: Nil))
// List of Integers
val nums = 1 :: (2 :: (3 :: (4 :: Nil)))
// Empty List.
val empty = Nil
// Two dimensional list
val dim = (1 :: (0 :: (0 :: Nil))) ::
(0 :: (1 :: (0 :: Nil))) ::
(0 :: (0 :: (1 :: Nil))) :: Nil
列表的基本操作
列表上的所有操作都可以用以下三種方法來表達。
| 序號 | 方法和描述 |
|---|---|
| 1 |
head 此方法返回列表的第一個元素。 |
| 2 |
tail 此方法返回包含除第一個元素之外的所有元素的列表。 |
| 3 |
isEmpty 如果列表為空,此方法返回true,否則返回false。 |
以下示例顯示如何使用上述方法。
示例
object Demo {
def main(args: Array[String]) {
val fruit = "apples" :: ("oranges" :: ("pears" :: Nil))
val nums = Nil
println( "Head of fruit : " + fruit.head )
println( "Tail of fruit : " + fruit.tail )
println( "Check if fruit is empty : " + fruit.isEmpty )
println( "Check if nums is empty : " + nums.isEmpty )
}
}
將上述程式儲存為Demo.scala。使用以下命令編譯和執行此程式。
命令
\>scalac Demo.scala \>scala Demo
輸出
Head of fruit : apples Tail of fruit : List(oranges, pears) Check if fruit is empty : false Check if nums is empty : true
連線列表
您可以使用:::運算子或List.:::()方法或List.concat()方法來新增兩個或多個列表。請參見以下示例:
示例
object Demo {
def main(args: Array[String]) {
val fruit1 = "apples" :: ("oranges" :: ("pears" :: Nil))
val fruit2 = "mangoes" :: ("banana" :: Nil)
// use two or more lists with ::: operator
var fruit = fruit1 ::: fruit2
println( "fruit1 ::: fruit2 : " + fruit )
// use two lists with Set.:::() method
fruit = fruit1.:::(fruit2)
println( "fruit1.:::(fruit2) : " + fruit )
// pass two or more lists as arguments
fruit = List.concat(fruit1, fruit2)
println( "List.concat(fruit1, fruit2) : " + fruit )
}
}
將上述程式儲存為Demo.scala。使用以下命令編譯和執行此程式。
命令
\>scalac Demo.scala \>scala Demo
輸出
fruit1 ::: fruit2 : List(apples, oranges, pears, mangoes, banana) fruit1.:::(fruit2) : List(mangoes, banana, apples, oranges, pears) List.concat(fruit1, fruit2) : List(apples, oranges, pears, mangoes, banana)
建立統一列表
您可以使用List.fill()方法建立一個包含零個或多個相同元素副本的列表。嘗試以下示例程式。
示例
object Demo {
def main(args: Array[String]) {
val fruit = List.fill(3)("apples") // Repeats apples three times.
println( "fruit : " + fruit )
val num = List.fill(10)(2) // Repeats 2, 10 times.
println( "num : " + num )
}
}
將上述程式儲存為Demo.scala。使用以下命令編譯和執行此程式。
命令
\>scalac Demo.scala \>scala Demo
輸出
fruit : List(apples, apples, apples) num : List(2, 2, 2, 2, 2, 2, 2, 2, 2, 2)
製表函式
您可以將函式與List.tabulate()方法一起使用,在製表列表之前將其應用於列表的所有元素。它的引數與List.fill的引數一樣:第一個引數列表給出要建立的列表的維度,第二個引數描述列表的元素。唯一的區別是,元素不是固定的,而是由函式計算的。
嘗試以下示例程式。
示例
object Demo {
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 )
}
}
將上述程式儲存為Demo.scala。使用以下命令編譯和執行此程式。
命令
\>scalac Demo.scala \>scala Demo
輸出
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))
反轉列表順序
您可以使用List.reverse方法反轉列表的所有元素。以下示例顯示了用法。
示例
object Demo {
def main(args: Array[String]) {
val fruit = "apples" :: ("oranges" :: ("pears" :: Nil))
println( "Before reverse fruit : " + fruit )
println( "After reverse fruit : " + fruit.reverse )
}
}
將上述程式儲存為Demo.scala。使用以下命令編譯和執行此程式。
命令
\>scalac Demo.scala \>scala Demo
輸出
Before reverse fruit : List(apples, oranges, pears) After reverse fruit : List(pears, oranges, apples)