📜  Scala – 矢量

📅  最后修改于: 2022-05-13 01:54:57.264000             🧑  作者: Mango

Scala – 矢量

Scala 是一种面向对象的编程语言,具有函数式和多范式支持。 Scala 生成字节码并在Java虚拟机上运行。 scala 中的向量是不可变的数据结构,为元素提供随机访问,类似于列表。但是,该列表无法随机访问元素。下面是 Scala 中对向量执行的一些操作的实现:

1. 创建一个向量:可以在 Scala 中使用 Vector()函数创建一个新向量,并提供括号中的元素。

例子:

Scala
import scala.collection.immutable._
object GFG {
    
    def main(args: Array[String]){
        
        // Creating vector
        var vector1 = Vector(2, 3, 4, 5)
  
        // Using println and foreach function
        // To print elements of vector
        println(vector1)
        vector1.foreach((element:Int) => print(element+" "))
    }
}


Scala
import scala.collection.immutable._
object GFG {
    
    def main(args: Array[String]){
        
        // Creating vector
        var vector1 = Vector(2, 3, 4, 5)
  
        // Adding new elements into new vector
        var newVector = vector1 :+ 10
        print("\nVector Elements after adding: ")
        newVector.foreach((element:Int) => print(element+" "))
          
        // Creating vector
        var vector2 = Vector(7, 100)
          
        // Merging two vectors
        var mergeVector = newVector ++ vector2
        print("\nVector Elements after merging: ")
        mergeVector.foreach((element:Int) => print(element+" "))
    }
}


Scala
import scala.collection.immutable._
object GFG {
    
    def main(args: Array[String]){
        
        // Creating vector
        var vector1 = Vector(2, 3, 4, 5)
        print("\nVector elements before reversing: ")
        vector1.foreach((element:Int) => print(element+" "))
  
        // Reverse vector elements
        var rev = vector1.reverse
        print("\nVector Elements after reversing: ")
        rev.foreach((element:Int) => print(element+" "))
    }
}


Scala
import scala.collection.immutable._
object GFG {
    
    def main(args: Array[String]){
        
        // Creating vector
        var vector1 = Vector(5, 1, 9, 100, 2, 25, 17)
        print("\nVector elements before sorting:")
        vector1.foreach((element:Int) => print(element+" "))
  
        // Sorting vector elements
        var st = vector1.sorted
        
        print("\nVector Elements after sorting: ")
        st.foreach((element:Int) => print(element+" "))
    }
}


输出:
Vector(2, 3, 4, 5)
2 3 4 5 

2. 向向量中添加元素: Scala 中可以使用:+运算符将单个元素添加到向量中,也可以使用++运算符将多个元素添加到向量中。

例子:

斯卡拉

import scala.collection.immutable._
object GFG {
    
    def main(args: Array[String]){
        
        // Creating vector
        var vector1 = Vector(2, 3, 4, 5)
  
        // Adding new elements into new vector
        var newVector = vector1 :+ 10
        print("\nVector Elements after adding: ")
        newVector.foreach((element:Int) => print(element+" "))
          
        // Creating vector
        var vector2 = Vector(7, 100)
          
        // Merging two vectors
        var mergeVector = newVector ++ vector2
        print("\nVector Elements after merging: ")
        mergeVector.foreach((element:Int) => print(element+" "))
    }
}
输出:
Vector Elements after adding: 2 3 4 5 10 
Vector Elements after merging: 2 3 4 5 10 7 100 

3. 反转向量元素:向量的元素可以使用 scala.collection.immutable 包中的 reverse函数按照它们插入的顺序反转。

例子:

斯卡拉

import scala.collection.immutable._
object GFG {
    
    def main(args: Array[String]){
        
        // Creating vector
        var vector1 = Vector(2, 3, 4, 5)
        print("\nVector elements before reversing: ")
        vector1.foreach((element:Int) => print(element+" "))
  
        // Reverse vector elements
        var rev = vector1.reverse
        print("\nVector Elements after reversing: ")
        rev.foreach((element:Int) => print(element+" "))
    }
}
输出:
Vector elements before reversing: 2 3 4 5 
Vector Elements after reversing: 5 4 3 2 

4. 向量元素的排序:向量的元素可以使用Scala中的sorted函数进行排序。

例子:

斯卡拉

import scala.collection.immutable._
object GFG {
    
    def main(args: Array[String]){
        
        // Creating vector
        var vector1 = Vector(5, 1, 9, 100, 2, 25, 17)
        print("\nVector elements before sorting:")
        vector1.foreach((element:Int) => print(element+" "))
  
        // Sorting vector elements
        var st = vector1.sorted
        
        print("\nVector Elements after sorting: ")
        st.foreach((element:Int) => print(element+" "))
    }
}
输出:
Vector elements before sorting:5 1 9 100 2 25 17 
Vector Elements after sorting: 1 2 5 9 17 25 100