To get a sublist in Kotlin, you can use the subList
method of the List
class.
In this example,
myList
with some elements.subList
method to get a sublist from myList
. This method takes two parameters: the starting index (inclusive) and the ending index (exclusive).subList
method returns a view of the specified range within the list.fun main() {
val myList = listOf(1, 2, 3, 4, 5)
val subList = myList.subList(1, 4)
println("Sublist: $subList")
}
Sublist: [2, 3, 4]
In this example,
myList
with some elements.subList
method to get a sublist from myList
by specifying different start and end indices.subList
method returns the elements within the specified range.fun main() {
val myList = listOf(10, 20, 30, 40, 50, 60)
val subList = myList.subList(2, 5)
println("Sublist: $subList")
}
Sublist: [30, 40, 50]
In this tutorial, we learned How to get Sub List in Kotlin language with well detailed examples.