Arrays and ranges
Array is the one collection here that is not immutable: it is a Java array, fixed in length and changed in place. A Range is the other extreme, a sequence of numbers that stores its start, its end and its step instead of its elements. The two are useful and both are easy to misuse, so this lesson shows what each one is and where the other collections are the better answer.
Array: fixed length, changed in place
An array is built with Array(...) or Array.ofDim, read and written with parentheses, and it has the collection methods of a sequence except the ones that would change its length. Printing it directly shows the JVM's name for the type, so a program converts it first:
arrays.scala
@main def arrays(): Unit =
val numbers = Array(3, 1, 2)
println(numbers.length)
println(numbers(0))
numbers(0) = 30
println(numbers.toList)
println(numbers.sorted.toList)
println(numbers.mkString(", "))
val grid = Array.ofDim[Int](2, 3)
grid(1)(2) = 7
println(grid.map(_.toList).toList)
val copied = numbers.clone()
copied(0) = 1
println(numbers(0))
println(copied(0))
scala-cli run --server=false arrays.scala
3
3
List(30, 1, 2)
List(1, 2, 30)
30, 1, 2
List(List(0, 0, 0), List(0, 0, 7))
30
1
numbers(0) = 30 changes the array the val holds: the name is fixed, its contents are not. toList makes an immutable copy, and clone makes a second array so that changing one does not change the other. An array is also invariant in its element type, which a List is not; the lesson on variance comes back to that. A program that only needs a sequence uses a Vector; an array is for the boundary with Java and for code where the copy really costs too much.
A range of numbers
1 to 5 includes five, 1 until 5 stops before it, and by gives the step. A range is an IndexedSeq, so every collection method works on it, and it stores its start, its end and its step rather than the numbers themselves:
ranges.scala
@main def ranges(): Unit =
println(1 to 5)
println(1 until 5)
println(0 to 10 by 2)
println((1 to 5).sum)
println((1 to 5).map(n => n * n))
println(('a' to 'e').mkString)
println((5 to 1 by -2).toList)
val thousand = 1 to 1000
println(thousand.length)
println(thousand.take(3).toList)
println(thousand.contains(500))
println((1 to 1000).count(_ % 100 == 0))
scala-cli run --server=false ranges.scala
Range 1 to 5
Range 1 until 5
Range 0 to 10 by 2
15
Vector(1, 4, 9, 16, 25)
abcde
List(5, 3, 1)
1000
List(1, 2, 3)
true
10
The range prints as Range 1 to 5 rather than as a list, because it holds three numbers and not five. map over a range returns a Vector, which is why the squares print that way. A range is how a loop over numbers is written in Scala: for i <- 1 to 10 needs no counter, and the collection it walks is a value like any other.
Related
- Loops and for comprehensions: the for a range is written in.
- Lists and vectors: the immutable sequences an array is not.
- map, flatMap, filter and fold: the methods a range answers.
- Java: the array both languages share, and the one place where Scala's is the same object.
Article author: Arthur Isaev