To remove an element at a specific index from a list in Kotlin, you can use the removeAt method.
In this example,
numbers
with integer values.removeAt
method of the list to remove the element at the specified index.fun main() {
val numbers = mutableListOf(1, 2, 3, 4, 5)
val indexToRemove = 2
numbers.removeAt(indexToRemove)
println("List after removing element at index $indexToRemove: $numbers")
}
List after removing element at index 2: [1, 2, 4, 5]
In this example,
words
with string values.removeAt
method of the list to remove the element at the first index.fun main() {
val words = mutableListOf("apple", "banana", "cherry")
words.removeAt(0)
println("List after removing element at first index: $words")
}
List after removing element at first index: [banana, cherry]
In this tutorial, we learned How to Remove Element at a Specific Index from a List in Kotlin language with well detailed examples.