Kotlin List toUShortArray()
Syntax & Examples
Syntax of List.toUShortArray()
The syntax of List.toUShortArray() extension function is:
fun Collection<UShort>.toUShortArray(): UShortArray
This toUShortArray() extension function of List returns an array of UShort containing all of the elements of this collection.
✐ Example
1 Example
In this example,
- We create a list named
list1
containing unsigned short integers100, 200, 300
. - We convert
list1
to a UShortArray using thetoUShortArray()
function. - The resulting UShortArray contains all elements from the list.
- Finally, we print the elements of
result
separated by commas to standard output.
Kotlin Program
fun main(args: Array<String>) {
val list1 = listOf(100.toUShort(), 200.toUShort(), 300.toUShort());
val result = list1.toUShortArray();
println(result.joinToString());
}
Output
100, 200, 300
Summary
In this Kotlin tutorial, we learned about toUShortArray() extension function of List: the syntax and few working examples with output and detailed explanation for each example.