The toDoubleArray() extension function in Kotlin returns an array of Double containing all of the elements of the collection.
The syntax of Set.toDoubleArray() extension function is:
fun Collection<Double>.toDoubleArray(): DoubleArray
This toDoubleArray() extension function of Set returns an array of Double containing all of the elements of this collection.
Set.toDoubleArray() returns value of type DoubleArray
.
Using toDoubleArray() to convert a set of Double values to a DoubleArray.
For example,
fun main() {
val doubleSet = setOf(1.1, 2.2, 3.3)
val doubleArray = doubleSet.toDoubleArray()
println(doubleArray.joinToString())
}
1.1, 2.2, 3.3
Using toDoubleArray() to handle an empty set of Double values.
For example,
fun main() {
val emptySet = emptySet<Double>()
val doubleArray = emptySet.toDoubleArray()
println(doubleArray.joinToString())
}
Using toDoubleArray() to convert a set of mixed Double values to a DoubleArray.
For example,
fun main() {
val mixedSet = setOf(1.1, -2.2, 3.3, -4.4)
val doubleArray = mixedSet.toDoubleArray()
println(doubleArray.joinToString())
}
1.1, -2.2, 3.3, -4.4
In this Kotlin tutorial, we learned about toDoubleArray() extension function of Set: the syntax and few working examples with output and detailed explanation for each example.