elect
11/11/2020, 3:28 PMVampire
11/11/2020, 3:35 PMfun main() {
val x = mutableListOf(5, 4, 3, 2, 1)
println(x)
x.subList(1, 4).sort()
println(x)
}
=>
[5, 4, 3, 2, 1]
[5, 2, 3, 4, 1]
CLOVIS
11/11/2020, 3:54 PMsubList
copied the list 🤔 that's very good to knowstephanmg
11/11/2020, 3:56 PMstephanmg
11/11/2020, 3:56 PMstephanmg
11/11/2020, 3:56 PMVampire
11/11/2020, 3:57 PMArrayList
, but it is basically the same, yes. 🙂Vampire
11/11/2020, 3:57 PMReturns a view of the portion of this list between the specified fromIndex (inclusive) and toIndex (exclusive). The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa.
Structural changes in the base list make the behavior of the view undefined.
Vampire
11/11/2020, 4:01 PMslice
would give you an independent listelect
11/11/2020, 4:01 PMstephanmg
11/11/2020, 4:11 PMVampire
11/11/2020, 4:12 PMfun main() {
val x = mutableListOf(5, 4, 3, 2, 1)
println(x)
val slice: List<Int> = x.slice(1..3)
println(slice)
(slice as MutableList).sort()
println(slice)
println(x)
}
=>
[5, 4, 3, 2, 1]
[4, 3, 2]
[2, 3, 4]
[5, 4, 3, 2, 1]
stephanmg
11/11/2020, 4:16 PM