Kotlin List InterfaceKotlin List is an interface and generic collection of elements. The List interface inherits form Collection<T> class. It is immutable and its methods supports only read functionalities. To use the List interface we need to use its function called listOf(), listOf<E>(). The elements of list follow the sequence of insertion order and contains index number same as array. List Interface DeclarationFunction of Kotlin List InterfaceThere are several functions are available in the List interface. Some functions of List interface are mention below.
Kotlin List Example 1Let's see an example of list using listOf() function. Output: Ajay Vijay Prakash Kotlin List Example 2In the listOf() function we can pass the different types of data at the same time. List can also traverse the list using index range. Output: 1 2 3 Ajay Vijay Prakash 1 2 3 Ajay Vijay Prakash Kotlin List Example 3For more specific we can provide the generic types of list such as listOf<Int>(), listOf<String>(), listOf<Any>() Let's see the example. Output: print int list 1 2 3 print string list Ajay Vijay Prakash print any list 1 2 3 Ajay Vijay Prakash Kotlin List Example 4Let's see the use of different function of Kotlin list interface using listOf<T>() function. Output: Ajay Vijay Prakash Vijay Rohan Ajay 1 3 5 true true [Prakash, Vijay] false [Vijay, Prakash, Vijay, Rohan] [Ajay, Vijay, Prakash] The limitation of List interface is that it is immutable. It cannot add more elements in list after its declaration. To solve this limitation Collection framework provide mutable list. Next TopicMutableList: mutableListOf() |