Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Data Structures  ›  Lesson

Vector and Array

Vector 12 minA Vector is an immutable sequence with fast access and update at any position, while an Array is a fixed-size, mutable Java array that is fast but compares by identity.

Vector and Array

The coach wants to look up and correct paces anywhere in the season's log: the 40th run, the 212th, the last. A List would walk its chain every time. Scala has two sequences built for jumping straight to a position, and they differ in one big way.

The rule

Vector: immutable and fast anywhere. Array: mutable, fixed size, Java's own.

A Vector is immutable like a List, but stores its elements in a wide, shallow tree, so reading or replacing any position takes about the same short time:

val paces = Vector(306, 312, 298, 301)   // seconds per km
paces(2)                                  // 298
val corrected = paces.updated(1, 305)     // Vector(306, 305, 298, 301)
paces                                     // unchanged: Vector(306, 312, 298, 301)

For an immutable sequence that you read by position, Vector is the usual default.

An Array is a fixed-size block of slots, exactly a Java array. It is mutable: you can overwrite a slot in place, even through a val:

val lapTimes = Array(71, 69, 74)   // seconds per lap
lapTimes(0) = 70                   // allowed: now 70, 69, 74
lapTimes.length                    // 3, and it can never grow

Arrays are the fastest option and are what many Java libraries expect, but they bring two Java habits that surprise people:

  • Equality is identity. Array(1, 2) == Array(1, 2) is false, because they are two separate objects. Use a.sameElements(b) to compare contents.
  • Printing is unhelpful. println(lapTimes) shows something like [I@1b6d3586. Use lapTimes.mkString(", ") to see 70, 69, 74.

The shortcut

Pick with two questions: Must it change in place? Do I need fast access by position?

  • No changes in place, walked from the front: List.
  • No changes in place, read at any position: Vector.
  • Changed in place, fixed size, raw speed or a Java library: Array.
Correcting the second paceStep 1 of 4
  1. Choose the type

    The paces are read and corrected by position, but should never change behind anyone's back: Vector.

Drills

Answer fast — trust the rule.

quick drill

What is Vector(306, 312, 298, 301).updated(1, 305)?

quick drill

val lapTimes = Array(71, 69, 74) then lapTimes(0) = 70. What happens?

quick drill

What is Array(1, 2) == Array(1, 2)?

quick drill

Which type fits an immutable season log that is read at random positions?

quick drill

How do you print an array's contents readably?

Remember

  • A Vector is immutable and fast at any position; an Array is mutable, fixed in size, and Java's own type.
  • Choose with two questions: must it change in place, and do you need access by position?
  • Trap: Arrays compare and print like Java objects, so use sameElements and mkString.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainList, Vector or Array?

Pick a sequence type for each: (a) the season's paces, read and corrected at random positions by the coach; (b) lap times handed to a Java timing library that overwrites them in place; (c) today's runs, added at the front and read from start to finish. Justify each choice and name one Array pitfall.