Kotlin List toULongArray()
Syntax & Examples
Syntax of List.toULongArray()
The syntax of List.toULongArray() extension function is:
fun Collection<ULong>.toULongArray(): ULongArrayThis toULongArray() extension function of List returns an array of ULong containing all of the elements of this collection.
✐ Examples
1 Example
In this example,
- We create a list named
list1containing unsigned longs1, 2, 3, 4, 5. - We convert
list1to a ULongArray using thetoULongArray()extension function. - We print the elements of the
ulongArrayjoined into a string to standard output.
Kotlin Program
fun main(args: Array<String>) {
val list1 = listOf(1uL, 2uL, 3uL, 4uL, 5uL)
val ulongArray = list1.toULongArray()
println(ulongArray.joinToString())
}Output
1, 2, 3, 4, 5
2 Example
In this example,
- We create a list named
list3containing strings"apple", "banana", "cherry". - We map each string in
list3to its length as an unsigned long usingmap { it.length.toULong() }and then convert the list to a ULongArray usingtoULongArray()extension function. - We print the elements of the
ulongArrayjoined into a string to standard output.
Kotlin Program
fun main(args: Array<String>) {
val list3 = listOf("apple", "banana", "cherry")
val ulongArray = list3.map { it.length.toULong() }.toULongArray()
println(ulongArray.joinToString())
}Output
5, 6, 6
Summary
In this Kotlin tutorial, we learned about toULongArray() extension function of List: the syntax and few working examples with output and detailed explanation for each example.