How to skip the first "n" entries in Kotlin
In this recipe, we will learn how to drop entries in a collection. First, we will see how to drop the first n items, then we will see how to drop the last n items, and finally, we will see how to use a predicate while dropping the elements from the collection.
Getting ready
I'll be using IntelliJ IDEA for writing and running Kotlin code; you are free to use any IDE that can do the same task.
How to do it…
In the following steps, we will learn to skip the first n entries of a Kotlin list:
- First, let's see how to drop the first n items in a collection. We will be using a list, but it also works with an array. Also, we will be using
kotlin.stdlib
, which contains the functions required in this recipe. The function to use here isdrop
:
fun main(args: Array<String>) {
val list= listOf<Int>(1,2,3,4,5,6,7,8,9)
var droppedList=list.drop(2)
droppedList.forEach {
print(" ${it} ")
}
}
//Output: 3 4 5 6 7 8 9
- To skip the last n...