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 的参数一样:第一个参数 list 给出要创建的列表的维度,第二个参数描述列表的元素。 唯一的区别是元素不是固定的,而是从函数计算的。
试试下面的示例程序。
示例
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)
Scala 列表方法
以下是重要的方法,您可以在玩列表时使用这些方法。 有关可用方法的完整列表,请查看 Scala 的官方文档。
| 序号 | 带有描述的方法 |
|---|---|
| 1 |
def +(elem: A): List[A] 在此列表中添加一个元素 |
| 2 |
def ::(x: A): List[A] 在此列表的开头添加一个元素。 |
| 3 |
def :::(prefix: List[A]): List[A] 在此列表前面添加给定列表的元素。 |
| 4 |
def ::(x: A): List[A] 在列表的开头添加一个元素 x |
| 5 |
def addString(b: StringBuilder): StringBuilder 将列表的所有元素附加到字符串生成器。 |
| 6 |
def addString(b: StringBuilder, sep: String): StringBuilder 使用分隔符字符串将列表的所有元素附加到字符串生成器。 |
| 7 |
def apply(n: Int): A 通过列表中的索引选择一个元素。 |
| 8 |
def contains(elem: Any): Boolean 测试列表是否包含给定值作为元素。 |
| 9 |
def copyToArray(xs: Array[A], start: Int, len: Int): Unit 将列表的元素复制到数组。 从位置 start 开始,最多用此列表的长度 (len) 个元素填充给定数组 xs。 |
| 10 |
def distinct: List[A] 从列表中构建一个没有任何重复元素的新列表。 |
| 11 |
def drop(n: Int): List[A] 返回除前 n 个元素之外的所有元素。 |
| 12 |
def dropRight(n: Int): List[A] 返回除最后 n 个元素之外的所有元素。 |
| 13 |
def dropWhile(p: (A) => Boolean): List[A] 删除满足谓词的元素的最长前缀。 |
| 14 |
def endsWith[B](that: Seq[B]): Boolean 测试列表是否以给定的序列结尾。 |
| 15 |
def equals(that: Any): Boolean 任意序列的equals方法。 将此序列与其他对象进行比较。 |
| 16 |
def exists(p: (A) => Boolean): Boolean 测试谓词是否适用于列表的某些元素。 |
| 17 |
def filter(p: (A) => Boolean): List[A] 返回列表中满足谓词的所有元素。 |
| 18 |
def forall(p: (A) => Boolean): Boolean 测试谓词是否适用于列表的所有元素。 |
| 19 |
def foreach(f: (A) => Unit): Unit 将函数 f 应用于列表的所有元素。 |
| 20 |
def head: A 选择列表的第一个元素。 |
| 21 |
def indexOf(elem: A, from: Int): Int 查找列表中第一个出现值的索引,在索引位置之后。 |
| 22 |
def init: List[A] 返回除最后一个以外的所有元素。 |
| 23 |
def intersect(that: Seq[A]): List[A] 计算列表和另一个序列之间的多集交集。 |
| 24 |
def isEmpty: Boolean 测试列表是否为空。 |
| 25 |
def iterator: Iterator[A] 在可迭代对象中包含的所有元素上创建一个新的迭代器。 |
| 26 |
def last: A 返回最后一个元素。 |
| 27 |
def lastIndexOf(elem: A, end: Int): Int 查找列表中某个值最后一次出现的索引; 在给定的结束索引之前或之后。 |
| 28 |
def length: Int 返回列表的长度。 |
| 29 |
def map[B](f: (A) => B): List[B] 通过将函数应用于此列表的所有元素来构建新集合。 |
| 30 |
def max: A 找到最大的元素。 |
| 31 |
def min: A 找到最小的元素。 |
| 32 |
def mkString: String 以字符串形式显示列表的所有元素。 |
| 33 |
def mkString(sep: String): String 使用分隔符字符串显示列表中的所有元素。 |
| 34 |
def reverse: List[A] 以相反的顺序返回元素的新列表。 |
| 35 |
def sorted[B >: A]: List[A] 根据 Ordering 对列表进行排序。 |
| 36 |
def startsWith[B](that: Seq[B], offset: Int): Boolean 测试列表是否包含给定索引处的给定序列。 |
| 37 |
def sum: A 总结这个集合的元素。 |
| 38 |
def tail: List[A] 返回除第一个元素之外的所有元素。 |
| 39 |
def take(n: Int): List[A] 返回前"n"个元素。 |
| 40 |
def takeRight(n: Int): List[A] 返回最后"n"个元素。 |
| 41 |
def toArray: Array[A] 将列表转换为数组。 |
| 42 |
def toBuffer[B >: A]: Buffer[B] 将列表转换为可变缓冲区。 |
| 43 |
def toMap[T, U]: Map[T, U] 将此列表转换为映射。 |
| 44 |
def toSeq: Seq[A] 将列表转换为序列。 |
| 45 |
def toSet[B >: A]: Set[B] 将列表转换为集合。 |
| 46 |
def toString(): String 将列表转换为字符串。 |

