Scala – Vector

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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+" "))
    }
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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+" "))
    }
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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+" "))
    }
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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+" "))
    }
}

chevron_right


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

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.