Kotlin List toFloatArray()
Syntax & Examples
Syntax of List.toFloatArray()
The syntax of List.toFloatArray() extension function is:
fun Collection<Float>.toFloatArray(): FloatArray
This toFloatArray() extension function of List returns an array of Float containing all of the elements of this collection.
✐ Examples
1 Example
In this example,
- We create a list named
floatList
containing float values1.2f, 3.4f, 5.6f
. - We use the
toFloatArray()
function onfloatList
to convert it to a FloatArray. - We then print the
floatArray
to standard output usingcontentToString()
.
Kotlin Program
fun main(args: Array<String>) {
val floatList = listOf(1.2f, 3.4f, 5.6f)
val floatArray = floatList.toFloatArray()
println(floatArray.contentToString())
}
Output
[1.2, 3.4, 5.6]
2 Example
In this example,
- We create a list named
floatList
containing float values1.1f, 2.2f, 3.3f
. - We use the
toFloatArray()
function onfloatList
to convert it to a FloatArray. - We then print the
floatArray
to standard output usingcontentToString()
.
Kotlin Program
fun main(args: Array<String>) {
val floatList = listOf(1.1f, 2.2f, 3.3f)
val floatArray = floatList.toFloatArray()
println(floatArray.contentToString())
}
Output
[1.1, 2.2, 3.3]
3 Example
In this example,
- We create a list named
floatList
containing float values0.0f, -1.5f, 4.8f
. - We use the
toFloatArray()
function onfloatList
to convert it to a FloatArray. - We then print the
floatArray
to standard output usingcontentToString()
.
Kotlin Program
fun main(args: Array<String>) {
val floatList = listOf(0.0f, -1.5f, 4.8f)
val floatArray = floatList.toFloatArray()
println(floatArray.contentToString())
}
Output
[0.0, -1.5, 4.8]
Summary
In this Kotlin tutorial, we learned about toFloatArray() extension function of List: the syntax and few working examples with output and detailed explanation for each example.