Kotlin Array.toLongArray()

Kotlin Array.toLongArray() Tutorial

The Array.toLongArray() function in Kotlin is used to convert an Array of Long type values, into a LongArray. It creates a new LongArray containing the elements of the given original array.

This is just a conversion of data from Array<Long> type to LongArray type.

This tutorial will explore the syntax of the Array.toLongArray() function and provide examples of its usage in Kotlin arrays.

Syntax

The syntax of the Array.toLongArray() function is as follows:

fun Array<out Long>.toLongArray(): LongArray

Examples for Array.toLongArray() function

1. Convert Array of long integer values to LongArray

In this example, we’ll take an array of long integer values in originalArray, and convert this Array into an LongArray using Array.toLongArray().

Kotlin Program

fun main() {
    val originalArray: Array<Long> = arrayOf(10000, 20000, 30000, 400000)

    // Using toLongArray() to convert the Array of long integers to a LongArray
    val longArray = originalArray.toLongArray()

    // Printing the original array and the resulting LongArray
    println("Given Array:\n${originalArray.contentToString()}\n")
    println("Long Array:\n${longArray.contentToString()}")
}

Output

Given Array:
[10000, 20000, 30000, 400000]

Long Array:
[10000, 20000, 30000, 400000]

Summary

In this tutorial, we’ve covered the Array.toLongArray() function in Kotlin, its syntax, and how to use it to convert an Array of Long values into an LongArray.