Home »
Scala language
Vectors in Scala
In this article, we will learn about Vectors in Scala. All methods and ways to create vectors in Scala.
Submitted by Shivang Yadav, on September 01, 2020
A vector in programming is a 1-D data structure that is similar to an array. It is used for storing elements in the program. It shares all features with an array but its size can be changed.
In Scala, Vectors are immutable-indexed data structures that provide random access to the elements of the vectors. The vector in Scala shares properties with the list.
Vector being indexed data structure provides quick and random access to the elements of the data structure.
Example to vector in Scala:
// A valid vector in scala
Vector(891, 13, 56)
How to create a vector in Scala?
You can create a vector in Scala using the following syntaxes:
val vector_name = Vector(element1, element2, ... )
val vector_name = Vector[data_type](element1, element2, ... )
val vector_name = Vector.apply(element1, element2, ... )
Program to create a vector in Scala
object MyClass {
def main(args: Array[String]) {
val num1 = Vector("C++", "Python", "Scala")
println("String Vector num1: " + num1)
val num2 = Vector[Int](45, 14, 9)
println("Integer Vector num2: " + num2)
val num3 = Vector.apply(34.12, 4.5, 5.1)
println("Number Vector num3: " + num3)
val num4 = Vector.apply[Char]('m', 'i', 's')
println("Charcter Vector num4: " + num4)
}
}
Output:
String Vector num1: Vector(C++, Python, Scala)
Integer Vector num2: Vector(45, 14, 9)
Number Vector num3: Vector(34.12, 4.5, 5.1)
Charcter Vector num4: Vector(m, i, s)
In this code, we have seen different methods to create a vector in Scala.
How to add (append and prepend) element to a vector?
In Scala, vector is immutable i.e. adding new elements to the same vector is not possible. So, you need to assign the appended elements to a new vector.
These methods are used to add elements to the vector:
Append element to vector:
- Append single element to vector ( :+ )
syntax: new_vector = old_vector :+ element
- Append multiple elements to vector ( ++ )
syntax: new_vector = old_vector :+ elements
Prepend element to vector:
- Prepend single element to vector ( +: )
syntax: new_vector = element +: old_vector
- Prepend multiple elements to vector ( ++: )
syntax: new_vector = element ++: old_vector
Program:
object MyClass {
def main(args: Array[String]) {
// Creating a vector
val vect = Vector[Int](45, 14, 9)
println("Initial vector : " + vect)
//Appending single element ot the vector
//using (:+) operator
println("\nAppending one element to the vector!")
val newvect1 = vect :+ 32
println("Updated vector : " + newvect1)
//Appending multiple elements ot the vector
//using (:+) operator
println("\nAppending multiple elements to the vector!")
val newvect2 = newvect1 ++ List(2, 54, 90)
println("Updated vector : " + newvect2)
//Prepending single element ot the vector
//using (+:) operator
println("\nPrepending one element to the vector!")
val newvect3 = 6 +: newvect2
println("Updated vector : " + newvect3)
//Prepending multiple elements ot the vector
//using (++:) operator
println("\nPrepending multiple elements to the vector!")
val newvect4 = List(1, 5) ++: newvect3
println("Updated vector : " + newvect4)
}
}
Output:
Initial vector : Vector(45, 14, 9)
Appending one element to the vector!
Updated vector : Vector(45, 14, 9, 32)
Appending multiple elements to the vector!
Updated vector : Vector(45, 14, 9, 32, 2, 54, 90)
Prepending one element to the vector!
Updated vector : Vector(6, 45, 14, 9, 32, 2, 54, 90)
Prepending multiple elements to the vector!
Updated vector : Vector(1, 5, 6, 45, 14, 9, 32, 2, 54, 90)
In the above code, we have created a vector and then added elements to the array using the append and prepend operators available in the Scala library. Here, we have to create a new vector every time we need to add a new element as vectors in Scala are immutable data structures.