Kotlin Array.toUShortArray()

Kotlin Array.toUShortArray() Tutorial

The Array.toUShortArray() function in Kotlin is used to convert an Array of UShort type values, into a UShortArray. It creates a new UShortArray containing the elements of the given original array.

This is just a conversion of data from Array<UShort> type to UShortArray type.

This tutorial will explore the syntax of the Array.toUShortArray() function and provide examples of its usage in Kotlin arrays.

Syntax

The syntax of the Array.toUShortArray() function is as follows:

fun Array<out UShort>.toUShortArray(): UShortArray

Examples for Array.toUShortArray() function

1. Convert Array of unsigned short integer values to UShortArray

In this example, we’ll take an array of unsigned short integer values in originalArray, and convert this Array into an UShortArray using Array.toUShortArray().

Kotlin Program

fun main() {
    val originalArray: Array<UShort> = arrayOf(10u, 20u, 30u, 40u)

    // Using toUShortArray() to convert the Array of short integers to a UShortArray
    val ushortArray = originalArray.toUShortArray()

    // Printing the original array and the resulting UShortArray
    println("Given Array:\n${originalArray.contentToString()}\n")
    println("UShort Array:\n${ushortArray.contentToString()}")
}

Output

Given Array:
[10, 20, 30, 40]

UShort Array:
[10, 20, 30, 40]

Summary

In this tutorial, we’ve covered the Array.toUShortArray() function in Kotlin, its syntax, and how to use it to convert an Array of UShort values into an UShortArray.