Kotlin longArrayOf()

Kotlin longArrayOf()

In Kotlin, the longArrayOf() function is used to create an array of long integers with specified values.

This function provides a convenient way to initialize an array of long integers without explicitly specifying the array type or size.

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

Syntax

The syntax of the longArrayOf() function is:

fun longArrayOf(vararg elements: Long): LongArray

The longArrayOf() function allows you to pass a variable number of long integer values, and it returns an array of long integers (LongArray) containing those values.

Examples for longArrayOf() function

1. Creating Array with longArrayOf() from given values

In this example, we’ll use longArrayOf() to create arrays with specified long values.

Kotlin Program

fun main() {
    // Creating an array with three long integers
    val array1 = longArrayOf(100L, 200L, 300L)

    // Printing the array
    println("Array: ${array1.joinToString(", ")}")
}

Output

Array: 100, 200, 300

2. Creating Empty Long Array with longArrayOf()

In this example, we’ll use longArrayOf() to create an empty array of long values.

Kotlin Program

fun main() {
    // Creating an empty array that store long values
    val array1 = longArrayOf()

    // Printing the array
    println("Array: ${array1.joinToString(", ")}")
}

Output

Array: 

Summary

In this tutorial, we’ve covered the longArrayOf() function in Kotlin, its syntax, and how to use it for creating arrays of long integers with specified values.