Kotlin floatArrayOf()

Kotlin floatArrayOf()

The floatArrayOf() function in Kotlin is used to create an array of Float with the specified values. It provides a concise way to initialize a float array.

In this tutorial, we shall learn the syntax and go through some examples of Kotlin floatArrayOf() function.

Syntax

The syntax of the floatArrayOf() function is:

fun floatArrayOf(vararg elements: Float): FloatArray

where

ParameterDescription
elementsThe values to be included in the float array.
Parameter of floatArrayOf() function

The function returns a float array containing the specified elements.

Examples

1. Using floatArrayOf() to Create a Float Array

In this example, we’ll use floatArrayOf() to create a float array with specified values.

Kotlin Program

fun main() {
    val floatArray = floatArrayOf(1.5f, 2.5f, 3.5f, 4.5f)
    
    // Display the created float array
    println("Created Float Array: ${floatArray.joinToString(", ")}")
}

Output

Created Float Array: 1.5, 2.5, 3.5, 4.5

In this example, floatArrayOf() is used to create a float array with specified values, and the array is displayed using joinToString().

Summary

The floatArrayOf() function in Kotlin is a convenient way to initialize a float array with specified values. It simplifies the process of creating float arrays with a concise syntax.