Skip to content
Related Articles

Related Articles

Improve Article

Scala – Vector

  • Last Updated : 03 Jul, 2020

Scala is an object-oriented programming language with functional and multi-paradigm support. Scala generates byte code and runs on Java Virtual Machine. Vectors in scala are immutable data structures providing random access for elements and is similar to the list. But, the list has incompetence of random access of elements.  Below is an implementation of some of the operations performed on vectors in Scala:
 

1. Creating a vector: A new vector can be created in Scala using Vector() function and providing the elements in the parenthesis. 

Example:
 

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+" "))
    }
}
Output:
Vector(2, 3, 4, 5)
2 3 4 5 

2. Adding elements to the vector: A single element can be added to the vector in Scala using :+ operator and multiple elements can be added in the vector using ++ operator. 



Example: 
 

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+" "))
    }
}
Output:
Vector Elements after adding: 2 3 4 5 10 
Vector Elements after merging: 2 3 4 5 10 7 100 

3. Reversing vector elements: Elements of a vector can be reversed in the order they are inserted using reverse function present in scala.collection.immutable package. 

Example: 
 

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+" "))
    }
}
Output:
Vector elements before reversing: 2 3 4 5 
Vector Elements after reversing: 5 4 3 2 

4. Sorting elements of vector: Elements of a vector can be sorted using sorted function in Scala. 

Example: 
 

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+" "))
    }
}
Output:
Vector elements before sorting:5 1 9 100 2 25 17 
Vector Elements after sorting: 1 2 5 9 17 25 100 



My Personal Notes arrow_drop_up