Kotlin Array.toShortArray()

Kotlin Array.toShortArray() Tutorial

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

This is just a conversion of data from Array<Short> type to ShortArray type.

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


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

fun Array<out Short>.toShortArray(): ShortArray

Examples for Array.toShortArray() function

1. Convert Array of short integer values to ShortArray

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

Kotlin Program

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

    // Using toShortArray() to convert the Array of short integers to a ShortArray
    val shortArray = originalArray.toShortArray()

    // Printing the original array and the resulting ShortArray
    println("Given Array:\n${originalArray.contentToString()}\n")
    println("Short Array:\n${shortArray.contentToString()}")


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

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


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