The toUShortArray() extension function in Kotlin returns an array of UShort containing all of the elements of the collection.
The syntax of Set.toUShortArray() extension function is:
fun Collection<UShort>.toUShortArray(): UShortArray
This toUShortArray() extension function of Set returns an array of UShort containing all of the elements of this collection.
Set.toUShortArray() returns value of type UShortArray
.
Using toUShortArray() to convert a set of UShort values to a UShortArray.
For example,
fun main() {
val ushortSet = setOf<UShort>(1u, 2u, 3u)
val ushortArray = ushortSet.toUShortArray()
println(ushortArray.joinToString())
}
1, 2, 3
Using toUShortArray() to handle an empty set of UShort values.
For example,
fun main() {
val emptySet = emptySet<UShort>()
val ushortArray = emptySet.toUShortArray()
println(ushortArray.joinToString())
}
Using toUShortArray() to convert a set of mixed UShort values to a UShortArray.
For example,
fun main() {
val mixedSet = setOf<UShort>(1u, 65535u, 128u)
val ushortArray = mixedSet.toUShortArray()
println(ushortArray.joinToString())
}
1, 65535, 128
In this Kotlin tutorial, we learned about toUShortArray() extension function of Set: the syntax and few working examples with output and detailed explanation for each example.