Introduction to Scala
David Venturi
Curriculum Manager, DataCamp
Array
: mutable sequence of objects with the same typeList
: immutable sequence of objects with the same typeArray
scala> val players = Array("Alex", "Chen", "Marta")
players: Array[String] = Array(Alex, Chen, Marta)
List
scala> val players = List("Alex", "Chen", "Marta")
players: List[String] = List(Alex, Chen, Marta)
List
has methods, like all of Scala collections
There are many List
methods
myList.drop()
myList.mkString(", ")
myList.length
myList.reverse
scala> val players = List("Alex", "Chen", "Marta")
players: List[String] = List(Alex, Chen, Marta)
scala> val newPlayers = "Sindhu" :: players
newPlayers: List[String] = List(Sindhu, Alex, Chen, Marta)
scala> var players = List("Alex", "Chen", "Marta")
players: List[String] = List(Alex, Chen, Marta)
scala> players = "Sindhu" :: players
players: List[String] = List(Sindhu, Alex, Chen, Marta)
List
and returns the resulting List
scala> val players = List("Alex", "Chen", "Marta")
players: List[String] = List(Alex, Chen, Marta)
scala> val newPlayers = "Sindhu" :: players
newPlayers: List[String] = List(Sindhu, Alex, Chen, Marta)
Nil
is an empty listscala> Nil
res0: scala.collection.immutable.Nil.type = List()
Nil
and ::
scala> val players = "Alex" :: "Chen" :: "Marta" :: Nil
players: List[String] = List(Alex, Chen, Marta)
scala> val playersError = "Alex" :: "Chen" :: "Marta"
<console>:11: error: value :: is not a member of String
val playersError = "Alex" :: "Chen" :: "Marta"
:::
for concatenationval playersA = List("Sindhu", "Alex") val playersB = List("Chen", "Marta")
val allPlayers = playersA ::: playersB
println(playersA + " and " + playersB + " were not mutated,") println("which means " + allPlayers + " is a new List.")
List(Sindhu, Alex) and List(Chen, Marta) were not mutated,
which means List(Sindhu, Alex, Chen, Marta) is a new List.
Introduction to Scala