This example shows how to use slice() method to create a slice in Kotlin with an example.
A slice is a portion of a list. Slices can be created with the slice() method. The method takes indexes of the elements to be picked up.
Kotlin List contains() Example
In the example, we create a list of integers. From the list, we produce two slices.
package net.sourcecodeexamples.kotlin
fun main() {
val nums = listOf(1, 2, 3, 4, 5, 6)
val nums2 = nums.slice(1..3)
println(nums2)
val nums3 = nums.slice(listOf(3, 4, 5))
println(nums3)
}
Output:
[2, 3, 4]
[4, 5, 6]
Comments
Post a Comment