To declare a float array in Kotlin, you can use the floatArrayOf
function or initialize an array of floats.
In this example,
floatArrayOf
function to declare a float array named floatArray
with specific float values.floatArrayOf
function takes float values as arguments and creates an array with those values.fun main(args: Array<String>) {
val floatArray = floatArrayOf(1.1f, 2.2f, 3.3f, 4.4f, 5.5f)
println("Float array: ${floatArray.joinToString()}")
}
Float array: 1.1, 2.2, 3.3, 4.4, 5.5
In this example,
floatArray
using the FloatArray
constructor with the desired size.fun main(args: Array<String>) {
val floatArray = FloatArray(5)
floatArray[0] = 1.1f
floatArray[1] = 2.2f
floatArray[2] = 3.3f
floatArray[3] = 4.4f
floatArray[4] = 5.5f
println("Float array: ${floatArray.joinToString()}")
}
Float array: 1.1, 2.2, 3.3, 4.4, 5.5
In this tutorial, we learned How to Declare a Float Array in Kotlin language with well detailed examples.