Kotlin List toULongArray()
Syntax & Examples


Syntax of List.toULongArray()

The syntax of List.toULongArray() extension function is:

fun Collection<ULong>.toULongArray(): ULongArray

This 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 list1 containing unsigned longs 1, 2, 3, 4, 5.
  • We convert list1 to a ULongArray using the toULongArray() extension function.
  • We print the elements of the ulongArray joined 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 list3 containing strings "apple", "banana", "cherry".
  • We map each string in list3 to its length as an unsigned long using map { it.length.toULong() } and then convert the list to a ULongArray using toULongArray() extension function.
  • We print the elements of the ulongArray joined 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.