recoveryger.blogg.se

Kotlin immutable list
Kotlin immutable list






kotlin immutable list

Println("Index of 'two' : " + theList.indexOf("two"))

kotlin immutable list

The indexOf() method returns the index of the first occurrence of the specified element in the list, or -1 if the specified element is not contained in the list. The isEmpty() method returns true if the collection is empty (contains no elements), false otherwise. The contain() method can also be used to check the existence of an element in a list. The in operator can be used to check the existence of an element in a list. Println("Size of the list " + theList.size) Val theList = listOf("one", "two", null, "four", "five") We can use size property to get the total number of elements in a list: Note - here it works like this operator in Java. Lets study them one by one: Using toString() function There are various ways to loop through a Kotlin list. When you run the above Kotlin program, it will generate the following output: Val theMutableList = mutableListOf("one", "two", "three", "four") Val theList = listOf("one", "two", "three", "four")

kotlin immutable list

To prevent unwanted modifications, obtain read-only views of mutable lists by casting them to List. Creating Kotlin Listsįor list creation, use the standard library functions listOf() for read-only lists and mutableListOf() for mutable lists. Kotlin mutable or immutable lists can have duplicate elements. The elements of list can be accessed using indices. A Kotlin list can be either mutable ( mutableListOf) or read-only ( listOf). Kotlin list is an ordered collection of items.








Kotlin immutable list