Slice
Say we have a list of elements, as follows:
val numbers = (1..10).toList() // Prints [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
We can take only part of this list using slice()
:
println(numbers.slice((0..3))) // Prints [1, 2, 3, 4], last index is included
We're using Kotlin ranges, which is a nice syntax.
In Java, there's a subList()
method, which is similar, but not inclusive:
println(numbers.subList(0, 3)) // Prints [1, 2, 3], last index is excluded