🔴 Scala Beginner

What is the difference between List and Array in Scala?

Answer

In Scala, List is an immutable, linked list. It is the idiomatic Scala collection for functional programming. Prepend is O(1) with ::. Random access is O(n). It implements scala.collection.immutable.List. Array is a mutable, fixed-size array backed by a Java array. Random access is O(1). It is mutable by default. Use Array when you need Java interop, performance-critical indexed access, or mutable collections. Use List for most functional Scala code. Scala also has Vector: an immutable indexed sequence with effectively O(1) random access and O(1) append — often a better choice than List when you need indexed access. ArrayBuffer and ListBuffer are mutable builder alternatives. The Scala collections library provides a rich hierarchy with consistent APIs across mutable and immutable variants.