List and MutableList
List is one of the most commonly used collection data types. It is an implementation of the Collection
interface used to work with a group of ordered data.
Note
The data in a list may be ordered based on when it was added (like if we add 3
after 4
to an Int List
, then 4
will appear in the list before 3
, much like an array) or may even be ordered based on other ordering algorithms.
As we mentioned earlier, Kotlin distinguishes between mutable and read-only collection types; so, the List
interface, which is immutable, contains only the read-only functions, as follows:
fun get(index: Int):E
: This method is used to get an element from the list at the given index.fun indexOf(element: @UnsafeVariance E):Int
: This method is used to identify the index of an element in the list. This method will search for the specified element inside the whole list and return the position of the element if it's in the list. Otherwise, it will return-1
.fun listIterator(): ListIterator<E>
:...