
- Kotlin - Home
- Kotlin - Overview
- Kotlin - Environment Setup
- Kotlin - Architecture
- Kotlin - Basic Syntax
- Kotlin - Comments
- Kotlin - Keywords
- Kotlin - Variables
- Kotlin - Data Types
- Kotlin - Operators
- Kotlin - Booleans
- Kotlin - Strings
- Kotlin - Arrays
- Kotlin - Ranges
- Kotlin - Functions
- Kotlin Control Flow
- Kotlin - Control Flow
- Kotlin - if...Else Expression
- Kotlin - When Expression
- Kotlin - For Loop
- Kotlin - While Loop
- Kotlin - Break and Continue
- Kotlin Collections
- Kotlin - Collections
- Kotlin - Lists
- Kotlin - Sets
- Kotlin - Maps
- Kotlin Objects and Classes
- Kotlin - Class and Objects
- Kotlin - Constructors
- Kotlin - Inheritance
- Kotlin - Abstract Classes
- Kotlin - Interface
- Kotlin - Visibility Control
- Kotlin - Extension
- Kotlin - Data Classes
- Kotlin - Sealed Class
- Kotlin - Generics
- Kotlin - Delegation
- Kotlin - Destructuring Declarations
- Kotlin - Exception Handling
Kotlin Array - dropLastWhile() Function
The Kotlin array dropLastWhile() function is used to retrieve the list contains the all elements except the last n elements that satisfy the given predicate, or condition.
This function takes a greater than (>) operator and deletes all the element from the last to given condition. For example; dropLastWhile{it > 'x'} removes all elements following the x
Syntax
Following is the syntax of Kotlin array dropLastWhile() function −
fun <T> Array<out T>.dropLastWhile( predicate: (T) -> Boolean ): List<T>
Parameters
This function accepts a predicate function as a parameter. This predicate function represent the number of elements from the last index of an array that need to be dropped.
Return value
This function returns a list containing all elements left after being dropped.
Example 1
Following is the basic example to demonstrate the use of dropLastWhile() function −
fun main(args: Array<String>) { val number: Array<Int> = arrayOf(1, 2, 3, 4, 5, 6, 7, 8) val after_drop = number.dropLastWhile{it>5} println("list after dropped: $after_drop") }
Output
On execution of the above code we get the following result −
list after dropped: [1, 2, 3, 4, 5]
Example 2
Now, let's see another example. We create an array that stores strings. We then use the dropLastWhile() function to drop the last n elements while we get given predicate −
fun main(args: Array<String>) { val strings: Array<String> = arrayOf("hii", "Hello", "tutorix", "tutorialspoint") val after_drop = strings.dropLastWhile{it>"Hello"} println("list after dropped: $after_drop") }
Output
After execution of the above code we get the following output −
list after dropped: [hii, Hello]
Example 3
The example below creates an array of 26 characters. We then use dropLastWhile() to drop the last n element while we get the 'n' −
fun main(args: Array<String>) { // Create an array of characters from 'a' to 'z' val alphabet: Array<Char> = ('a'..'z').toList().toTypedArray() // Drop the last n elements to the given predicate val after_Drop = alphabet.dropLastWhile{it>'n'} println("List after dropping the last n elements: $after_Drop") }
Output
The above code produce following output −
List after dropping the first n elements: [m, n, o, p, q, r, s, t, u, v, w, x, y, z]