opkclicks.blogg.se

Kotlin sublist
Kotlin sublist











Let’s take a look at how the slice() method works: val sliceListUsingIndices = countries.slice(1.4)ĪssertEquals(4, sliceListUsingIndices.size) val sliceListUsingCollection = countries.slice(listOf(1, 4))ĪssertEquals(2, sliceListUsingCollection.size) 7. Unlike subList(), this method will create a new list with the subset of elements. We can use the slice() method to retrieve part of the list based on indices. Moreover, the Collection interface provides another method to retrieve parts of the list. Let’s have a look at an example to create a sublist: val subList = countries.subList(1, 4)

kotlin sublist

So, any structural changes in the original list make the behavior of the view undefined. The subList() method returns a view of the original list and will change with it.

kotlin sublist

The parameters for the method are used to define a specified range of the list between the fromIndex (inclusive) and toIndex (exclusive). We can use the subList() method to retrieve a part of the list.













Kotlin sublist