Scala中的序列
Scala|顺序
Scala中的序列是一个集合,用于按确定的顺序存储元素。它是索引数据存储区,它允许使用基于0索引的元素的索引来顺序访问其元素。
它是可迭代类的可迭代集合,从中可获取其属性。该返回的有序列表或向量。=在插入顺序被保持。
在Scala中创建序列
首先了解如何在Scala中创建序列。以下语法用于在Scala中创建列表,
语法:
1) Creating an empty sequence, var emptySeq: Seq[data_type] = Seq(); 2) Creating an Sequence with defining the data type, var mySeq: Seq[data_type] = Seq(element1, element2, ...) 3) Creating an Sequence without defining the data type, var mySeq = Seq(element1, element2, ...)
演示如何在Scala中创建序列的程序
// Program to show how to create a list is Scala object MyObject { def main(args: Array[String]) { // Creating an empty Sequence in Scala println("Creating an Empty Sequence!") var empSeq: Seq[Int] = Seq(); println("Empty Sequence : " + empSeq) // Creating a Sequence by defining its data type println("\nCreating a Sequence by defining its data type ") var mySeq: Seq[String] = Seq("Scala", "JavaScript", "Python"); println("Sequence of type 'String': " + mySeq) // Creating a Sequence without defining its data type println("\nCreating a Sequence without defining its data type ") var mySeq2 = Seq(4 ,1 , 6, 7, 2) println("Sequence without defining its data type: " + mySeq2) } }
输出:
Creating an Empty Sequence! Empty Sequence : List()Creating a Sequence by defining its data type Sequence of type 'String': List(Scala, JavaScript, Python)Creating a Sequence without defining its data type Sequence without defining its data type: List(4, 1, 6, 7, 2)
Scala中的序列类型
在scala中使用了特征序列的两种类型或子特征。
他们是:
索引序列:索引序列或IndexedSeq返回一个向量,该向量允许对元素的随机和快速访问。
线性序列:线性序列或LinerSeq返回一个List,该List允许快速访问仅第一个和最后一个元素,其余按顺序访问。
程序来说明不同类型的序列
// Program to illustrate different types of Sequence import scala.collection._ object MyObject { def main(args: Array[String]) { // Creating a Indexed Sequence in Scala val IndSeq = IndexedSeq(3, 6, 9, 12, 15, 18) println("Indexed Sequence: " + IndSeq) // Creating a Linear Sequence in Scala val LinSeq = LinearSeq(2, 4, 6, 8, 10, 12, 14) println("Linear Sequence: " + LinSeq) } }
输出:
Indexed Sequence: Vector(3, 6, 9, 12, 15, 18) Linear Sequence: List(2, 4, 6, 8, 10, 12, 14)
常用的序列类型是线性序列。如果仅使用seq关键字初始化序列,则会创建LinerSeq。
Scala序列的操作
Scala中的序列具有许多预定义的功能和要在其上执行的操作。尽管元素不能更改,因为它是不可变的。但是可以执行提取给定索引的元素,查找长度等操作。
程序以给定序列索引访问元素
// Program to access element at a // given index of the sequence object MyObject { def main(args: Array[String]) { // Creating a Sequence in Scala val mySeq = Seq(3, 6, 9, 12, 15, 18) println("Sequence: " + mySeq) // Access elements at a specific index println("Element at index 0: " + mySeq(0)) println("Element at index 4: " + mySeq(4)) } }
输出:
Sequence: List(3, 6, 9, 12, 15, 18) Element at index 0: 3 Element at index 4: 15
在Sequence上预定义的常用方法
length:定义序列长度的字段。length:定义序列长度的字段。
语法:
Sequence_name.length
reverse:用于返回新顺序的函数,该顺序为逆序。
语法:
Sequence_name.reverse
isEmpty:用于检查序列是否为空的函数。
语法:
Sequence_name.isEmpty
该程序演示了一些常用的Sequence方法的工作
// Program to access element at a given index of the sequence object MyObject { def main(args: Array[String]) { // Creating a Sequence in Scala val mySeq = Seq(3, 6, 9, 12, 15, 18) println("Sequence: " + mySeq) // Checking if the sequence is empty or not if(mySeq.isEmpty) println("\nThe given Sequence is empty") else println("\nThe given Sequence is not empty") // Finding the length of sequence val len = mySeq.length println("\nThe length of the seqeunce is " + len) // Reversing the sequence val revSeq = mySeq.reverse println("\nThe reverse of the seqeunce is " + revSeq) } }
输出:
Sequence: List(3, 6, 9, 12, 15, 18) The given Sequence is not empty The length of the seqeunce is 6 The reverse of the seqeunce is List(18, 15, 12, 9, 6, 3)
Scala为序列定义了更多方法,这些方法将在以后的文章中看到。