Kotlin intArrayOf()

Kotlin intArrayOf()

In Kotlin, the intArrayOf() function is used to create an array of integers with specified values.

This function provides a convenient way to initialize an array of integers without explicitly specifying the array type or size.

In this tutorial, we’ll explore the syntax of the intArrayOf() function and provide examples of its usage in Kotlin.

Syntax

The syntax of the intArrayOf() function is:

fun intArrayOf(vararg elements: Int): IntArray

The intArrayOf() function allows you to pass a variable number of integer values, and it returns an array of integers (IntArray) containing those values.

Examples for intArrayOf() function

1. Creating Integer Array with intArrayOf()

In this example, we’ll use intArrayOf() to create an array with specified integer values.

Kotlin Program

fun main() {
    // Creating an array with three integers
    val array1 = intArrayOf(100, 200, 300)

    // Printing the array
    println("Array : ${array1.joinToString(", ")}")
}

Output

Array : 100, 200, 300

2. Creating Empty Array with intArrayOf()

In this example, we’ll use intArrayOf() to create an empty integer array. We shall call intArrayOf() function, and pass no arguments to it.

Kotlin Program

fun main() {
    // Creating an empty array
    val array1 = intArrayOf()

    // Printing the array
    println("Array : ${array1.joinToString(", ")}")
}

Output

Array : 

Summary

In this tutorial, we’ve covered the intArrayOf() function in Kotlin, its syntax, and how to use it for creating arrays of integers with specified values.