Kotlin Array dropLast()

Kotlin Array dropLast()

In Kotlin, the Array dropLast() function is used to exclude the last N elements of the original array. It returns a new list with the specified number of elements dropped from the end of the given array.

This function is useful when you need to remove a certain number of elements from the end of an array.

In this tutorial, we’ll explore the syntax of the Array dropLast() function and provide examples of its usage in Kotlin.

Syntax

The syntax of the dropLast() function is as follows:

fun <T> Array<out T>.dropLast(n: Int): List<T>

where

ParameterDescription
nAn integer value.
Parameters of Array dropLast() function

The dropLast() function takes an integer n as an argument, indicating the number of elements to drop from the end of the array. It returns a new list containing the remaining elements.

Examples

1. Using dropLast() to Remove the Last 3 Numbers

In this example, we’ll use dropLast() to exclude the last 3 numbers from the original array of integers.

Kotlin Program

fun main() {
    val numbersArray = arrayOf(10, 20, 30, 40, 50, 60, 70)

    // Using dropLast() to remove the last 3 numbers
    val result = numbersArray.dropLast(3)

    // Printing the original array and the result
    println("Numbers: \n${numbersArray.joinToString(", ")}\n")
    println("Result (After Dropping Last 3 Numbers): \n${result.joinToString(", ")}")
}

Output

Numbers: 
10, 20, 30, 40, 50, 60, 70

Result (After Dropping Last 3 Numbers): 
10, 20, 30, 40

2. Using dropLast() to Exclude Elements from the End of a String Array

In this example, we’ll use dropLast() to exclude a specified number of elements from the end of a String array.

Kotlin Program

fun main() {
    val fruitsArray = arrayOf("apple", "banana", "orange", "grape", "kiwi")

    // Using dropLast() to exclude the last 2 fruits
    val result = fruitsArray.dropLast(2)

    // Printing the original array and the result
    println("Fruits: \n${fruitsArray.joinToString(", ")}\n")
    println("Result (After Dropping Last 2 Fruits): \n${result.joinToString(", ")}")
}

Output

Fruits: 
apple, banana, orange, grape, kiwi

Result (After Dropping Last 2 Fruits): 
apple, banana, orange

3. Using dropLast() with a Larger Number

In this example, we’ll use dropLast() with a larger number to skip more elements than the array contains, resulting in an empty array.

Kotlin Program

fun main() {
    val fruitsArray = arrayOf("apple", "banana", "orange", "grape", "kiwi")

    // Using dropLast() with a larger number to skip more elements than the array contains
    val result = fruitsArray.dropLast(10)

    // Printing the original array and the result
    println("Fruits: \n${fruitsArray.joinToString(", ")}\n")
    println("Result (After Dropping Last 10 Elements): \n${result.joinToString(", ")}")
}

Output

Fruits: 
apple, banana, orange, grape, kiwi

Result (After Dropping Last 10 Elements): 

Summary

In this tutorial, we’ve covered the dropLast() function in Kotlin arrays, its syntax, and how to use it to exclude a specified number of elements from the end.