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’s Array[Int], Java’s String[] is Array[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))

See Also opens in new page

Code editor is using Scalakata.com written by Guillaume Massé