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