List is used to collect items that usually consist of elements of multiple data types. An array is also a vital component that collects several items of the same data type. List cannot manage arithmetic operations. Array can manage arithmetic operations.
Lists are mutable(values can be changed) whereas tuples are immutable(values cannot be changed).
Sequences provide a method apply() for indexing, ranging from 0 up to the length of the sequence. Seq has many subclasses including Queue, Range, List, Stack, and LinkedList. A List is a Seq that is implemented as an immutable linked list.
Specific to Scala, a list is a collection which contains immutable data, which means that once the list is created, then it can not be altered. In Scala, the list represents a linked list. In a Scala list, each element need not be of the same data type.
The Scala List
is an immutable recursive data structure which is such a fundamental structure in Scala, that you should (probably) be using it much more than an Array
(which is actually mutable - the immutable analog of Array
is IndexedSeq
).
If you are coming from a Java background, then the obvious parallel is when to use LinkedList
over ArrayList
. The former is generally used for lists which are only ever traversed (and whose size is not known upfront) whereas the latter should be used for lists which either have a known size (or maximum size) or for which fast random access is important.
ListBuffer
provides a constant-time conversion to a List
which is reason alone to use ListBuffer
if such later conversion is required.
A scala Array
should be implemented on the JVM by a Java array, and hence an Array[Int]
may be much more performant (as an int[]
) than a List[Int]
(which will box its contents, unless you are using the very latest versions of Scala which have the new @specialized
feature).
However, I think that the use of Array
s in Scala should be kept to a minimum because it feels like you really need to know what is going on under the hood to decide whether your array really will be backed by the required primitive type, or may be boxed as a wrapper type.
In addition to the answers posted already, here are some specifics.
While an Array[A]
is literally a Java array, a List[A]
is an immutable data structure that is either Nil
(the empty list) or consists of a pair (A, List[A])
.
Performance differences
Array List
Access the ith element θ(1) θ(i)
Delete the ith element θ(n) θ(i)
Insert an element at i θ(n) θ(i)
Reverse θ(n) θ(n)
Concatenate (length m,n) θ(n+m) θ(n)
Count the elements θ(1) θ(n)
Memory differences
Array List
Get the first i elements θ(i) θ(i)
Drop the first i elements θ(n-i) θ(1)
Insert an element at i θ(n) θ(i)
Reverse θ(n) θ(n)
Concatenate (length m,n) θ(n+m) θ(n)
So unless you need rapid random access, need to count elements, or for some reason you need destructive updates, a List
is better than an Array
.
An Array is mutable, meaning you can change the values of each index, while a List (by default) is immutable, meaning that a new list is created every time you do a modification. In most cases it is a more "functional" style to work with immutable datatypes and you should probably try and use a List with constructs like yield
, foreach
, match
and so forth.
For performance characteristics, an Array is faster with random access to elements, whereas a List is faster when prepending (adding) new elements. Iterating over them is comparable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With