Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 330 Vote(s) - 3.65 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Take last n element in Kotlin

#1
I have a list on which I'd like to apply some transformations, but excluding the first 2 elements. How can I do it the nicest way? Something like this:

list.reversed().take(list.size - 2)...(my transformations)

or

list.excludeFirstN(2)...(my transformations)

Reply

#2
Oh, I found the [drop()][1] function.



[1]:

[To see links please register here]

Reply

#3
You can use [takeLast(n)][1] like:

list.takeLast(3)

But be carful, the last number is represented first, so you may need to use [reversed][2] as well, so your code could be:

list.takeLast(3).reversed()


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#4
As already mentioned, `drop` is a good fit for the use case:


listOf(1,2,3,4).drop(2).forEach(::println)

Otherwise, you can also filter by index:

listOf(1,2,3,4).filterIndexed { index, _ -> index > 1 }.forEach(::println)
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through