listOf(1, 2, 3).slice(0..5) Instead of getting an...
# announcements
c
listOf(1, 2, 3).slice(0..5) Instead of getting an ArrayIndexOutOfBoundxException, how can I get (1, 2, 3)
d
First thing that comes to mind is:
Copy code
list.slice(0..min(list.lastIndex, 5))
👍 1
r
take(5)
if you need just first
d
If you use
take(5)
you are changing the performance characteristics of
slice
a lot, since
slice(IntRange)
can use
subList
, where as
slice(Iterable<Int>)
does not.
a
but
slice(IntRange)
does
toList()
so, what's the difference in perf?