ArrayList is a class that happens to implement the MutableList interface. The only difference is that arrayListOf() returns the ArrayList as an actual ArrayList. mutableListOf() returns a MutableList, so the actual ArrayList is "disguised" as just the parts that are described by the MutableList interface.
A Mutable List is an interface and generic collection of elements. Once a collection is declared as mutable, it becomes dynamic and we can modify its data as per requirement. A mutable list grows automatically in size as we insert new elements into it. The Mutable List inherits form the Generic<T> class.
Types of ImmutabilityMutable – The contents of the list can be freely changed. Read-Only – The contents of the collection are not meant to be changed. However, the underlying data can be changed. Immutable – Nothing can change the contents of the collection.
To add an element to a Mutable List in Kotlin, we can use add(element), or add(index, element) functions. add(element) adds element to the end of this Mutable List. add(index, element) adds element to this Mutable List at the given index.
From docs:
List: A generic ordered collection of elements. Methods in this interface support only read-only access to the list; read/write access is supported through the MutableList interface.
MutableList: A generic ordered collection of elements that supports adding and removing elements.
You can modify a MutableList: change, remove, add... its elements. In a List you can only read them.
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