give me a kotlin function which selects a page from list by given page number and size
- User
give me a kotlin function which selects a page from list by given page number and size
Assistant
Here's a simple Kotlin function that takes a list, page number, and page size as input and returns a sublist of the specified page:
kotlinfun <T> getPage(list: List<T>, page: Int, pageSize: Int): List<T> { if (page < 0 || pageSize <= 0 || pageSize > list.size) { throw IllegalArgumentException("Invalid page or page size") } val startIndex = page * pageSize return if (startIndex >= list.size) { // Return an empty list if the requested page is out of bounds emptyList() } else { list.subList(startIndex, minOf(startIndex + pageSize, list.size)) } }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.