- 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 集合 - 选项
- Scala 集合 - 队列
- Scala 集合 - 元组
- Scala 集合 - Seq
- Scala 集合 - 堆栈
- Scala 集合 - Stream
- Scala 集合组合器方法
- Scala 集合 - drop
- Scala 集合 - dropWhile
- Scala 集合 - 过滤器
- Scala 集合 - 查找
- Scala 集合 - flatMap
- Scala 集合 - 展平
- Scala 集合 - 折叠
- Scala 集合 - FoldLeft
- Scala 集合 - FoldRight
- Scala 集合 - 地图
- Scala 集合 - 分区
- Scala 集合 - 减少
- Scala 集合 - 扫描
- 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 |
头 此方法返回列表的第一个元素。 |
| 2 |
尾巴 此方法返回一个包含除第一个元素之外的所有元素的列表。 |
| 3 |
是空的 如果列表为空,则此方法返回 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)