22 Arrays
- Arrays are constructed simply using
Array(element1, element2, ...)
- Arrays in Scala map to Java primitive Arrays (e.g. Java’s
int[]
is Scala’sArray[Int]
, Java’sString[]
isArray[String]
in Scala) - Arrays are mutable (can’t change it’s size once created, but can modify it’s elements)
- Since they are using Java’s arrays, to print nicely an Array’s content, use
.mkString(",")
-
Array elements can be of any type, but the Array’s final type will be the lowest common denominator
class Foo(val value1:Int) class Bar(value1:Int, val value2:Int) extends Foo(value1) val list:Array[Foo] = Array(new Foo(1), new Bar(2,3))