Mutable list interfaces

This is the most powerful collection among all the lists in this hierarchy because it inherits all the functions from both sides â€“ from the MutableCollection interface as well as the List interface, and it contains its own functions too. We can declare a mutable list interface by using the MutableList keyword.

Take a look at the following example:

val mutableListValues : MutableList<Int> = mutableListOf(1,2,3,4,5)

Take a look at the following diagram, which shows the MutableList interface:

Mutable list interfaces provide the following functions:

  • add(index, item): As we have seen, the MutableCollection interface provides the add function, which inserts the element at the end of the list. The MutableList interface, however, provides an add function that inserts a value at a specific location.

For example, the following function will insert element 0 at the beginning of the list:

mutableListValues.add(0,0)

The first parameter represents the list index and the second parameter is an element to insert at the specified index. For example, we can add item 9 at index 3 as follows:

mutableListValues.add(3,9)

Add item 6 at the end of the list, as follows:

mutableListValues.add(6)
mutableListValues.add(mutableListValues.size,6)

When inserting an item in a list, make sure that the index is within the correct range. Kotlin will throw an out-of-bounds exception if, for example, the list contains 5 elements and the item is inserted at index 9.

  • addAll (index, list): The addAll function allows us to insert a new list at a specified location. It takes an index and a list. It returns true if the operation is successful, otherwise it returns false:
fun mutableLisFunctionAddAll(){
val mutableListValues : MutableList<Int> = mutableListOf(1,2,3,4,5)
println(mutableListValues)

var miniCollection = listOf(9,9,9)
if (mutableListValues.size > 1) {
var result = mutableListValues.addAll(1,miniCollection)
println("Mini list is added = $result")
}
println(mutableListValues)
}

Here, a list is initialized with the values (1, 2, 3, 4, 5). Then, we create another list. Before inserting the new list into the main list, we first verify whether the list size is more than one. This is because we want to insert it at location 1. Once the list is inserted successfully, we get the following result:

Mini list is added = true
[1, 9, 9, 9, 2, 3, 4, 5]

  • set(index, item): The set function replaces the old value with a new one at the specific index. The set function takes two parameters—the index and the new value to replace. It returns the old value at the specified index. The following example will replace element 1 with 5 at index 0:
fun mutableLisFunctionSetRemove(){
val mutableListValues : MutableList<Int> = mutableListOf(1,2,3,4,5)
println(mutableListValues)

val replaceWith = 5
val index = 0
val replaced = mutableListValues.set(index, replaceWith)

println("Element $replaced is replaced with element $replaceWith at index $index")
println(mutableListValues)
}

An alternative way to update the list element is as follows:

mutableListValues[0] = 5
  • removeAt(index): The removedAt function removes the element from the specified index. This function takes an index as a parameter and returns the removed value:
var index = 0
var removed = mutableListValues.removeAt(index)
println("Element $removed is removed at index $index")
println(mutableListValues)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.52.188