
- 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 - drop() Function
The Kotlin array drop() function is used to retrieve the list contains the all elements except the first nth element from an array. n is the number that must be passed to the drop function in order to remove the element from first to n. Following is the exceptions of this function −
- IllegalArgumentException: This exception is thrown if n is negative.
Syntax
Following is the syntax of Kotlin array drop() function −
fun <T> Iterable<T>.drop(n: Int): List<T>
Parameters
This function accepts a parameter (n), which represents the number of elements from the beginning of the array that should be dropped.
Return value
This function returns a list containing all the array elements left after being dropped.
Example 1
Following is the basic example to demonstrate the use of drop() function −
fun main(args: Array<String>) { val number: Array<Int> = arrayOf(1, 2, 3, 4, 5, 6, 7, 8) val after_drop = number.drop(4) println("list after dropped: $after_drop") }
Output
On execution of the above code we get the following result −
list after dropped: [5, 6, 7, 8]
Example 2
Now, let's see another example. We create an array that stores string. We then use the drop() function to drop first 2 elements −
fun main(args: Array<String>) { val number: Array<String> = arrayOf("hii", "Hello", "tutorix", "tutorialspoint") val after_drop = number.drop(2) println("list after dropped: $after_drop") }
Output
After execution of the above code we get the following output −
list after dropped: [tutorix, tutorialspoint]
Example 3
The example below creates an array of 26 characters. We then use drop() to drop first 20 element −
fun main(args: Array<String>) { // Create an array of characters from 'a' to 'z' val alphabet: Array<Char> = ('a'..'z').toList().toTypedArray() // Drop the first 20 elements from the array val after_Drop = alphabet.drop(20) println("List after dropping the first 20 elements: $after_Drop") }
Output
The above code produce following output −
List after dropping the first 20 elements: [u, v, w, x, y, z]