Outdated Notice
The Vector class is an indexed, immutable sequence. The “indexed” part of the description means that you can access Vector
elements very rapidly by their index value, such as accessing listOfPeople(999999)
.
In general, except for the difference that Vector
is indexed and List
is not, the two classes work the same, so we’ll run through these examples quickly.
Here are a few ways you can create a Vector
:
val nums = Vector(1, 2, 3, 4, 5)
val strings = Vector("one", "two")
val peeps = Vector(
Person("Bert"),
Person("Ernie"),
Person("Grover")
)
Because Vector
is immutable, you can’t add new elements to it. Instead you create a new sequence by appending or prepending elements to an existing Vector
. For instance, given this Vector
:
val a = Vector(1,2,3)
you append elements like this:
val b = a :+ 4
and this:
val b = a ++ Vector(4, 5)
The REPL shows how this works:
scala> val a = Vector(1,2,3)
a: Vector[Int] = Vector(1, 2, 3)
scala> val b = a :+ 4
b: Vector[Int] = Vector(1, 2, 3, 4)
scala> val b = a ++ Vector(4, 5)
b: Vector[Int] = Vector(1, 2, 3, 4, 5)
You can also prepend elements like this:
val b = 0 +: a
and this:
val b = Vector(-1, 0) ++: a
Once again the REPL shows how this works:
scala> val b = 0 +: a
b: Vector[Int] = Vector(0, 1, 2, 3)
scala> val b = Vector(-1, 0) ++: a
b: Vector[Int] = Vector(-1, 0, 1, 2, 3)
Because Vector
is not a linked-list (like List
), you can prepend and append elements to it, and the speed of both approaches should be similar.
Finally, you loop over elements in a Vector
just like you do with an ArrayBuffer
or List
:
scala> val names = Vector("Joel", "Chris", "Ed")
val names: Vector[String] = Vector(Joel, Chris, Ed)
scala> for (name <- names) println(name)
Joel
Chris
Ed
Contributors to this page:
Contents
- Introduction
- Prelude꞉ A Taste of Scala
- Preliminaries
- Scala Features
- Hello, World
- Hello, World - Version 2
- The Scala REPL
- Two Types of Variables
- The Type is Optional
- A Few Built-In Types
- Two Notes About Strings
- Command-Line I/O
- Control Structures
- The if/then/else Construct
- for Loops
- for Expressions
- match Expressions
- try/catch/finally Expressions
- Scala Classes
- Auxiliary Class Constructors
- Supplying Default Values for Constructor Parameters
- A First Look at Scala Methods
- Enumerations (and a Complete Pizza Class)
- Scala Traits and Abstract Classes
- Using Scala Traits as Interfaces
- Using Scala Traits Like Abstract Classes
- Abstract Classes
- Scala Collections
- The ArrayBuffer Class
- The List Class
- The Vector Class
- The Map Class
- The Set Class
- Anonymous Functions
- Common Sequence Methods
- Common Map Methods
- A Few Miscellaneous Items
- Tuples
- An OOP Example
- sbt and ScalaTest
- The most used scala build tool (sbt)
- Using ScalaTest with sbt
- Writing BDD Style Tests with ScalaTest and sbt
- Functional Programming
- Pure Functions
- Passing Functions Around
- No Null Values
- Companion Objects
- Case Classes
- Case Objects
- Functional Error Handling in Scala
- Concurrency
- Scala Futures
- Where To Go Next