Kotlin booleanArrayOf()

Kotlin booleanArrayOf()

The booleanArrayOf() function in Kotlin is used to create an array of Boolean with the specified values. It provides a concise way to initialize a boolean array.

In this tutorial, we shall learn the syntax and go through some examples of Kotlin booleanArrayOf() function.

Syntax

The syntax of the booleanArrayOf() function is:

fun booleanArrayOf(vararg elements: Boolean): BooleanArray

where

ParameterDescription
elementsThe values to be included in the boolean array.
Parameter of booleanArrayOf() function

The function returns a boolean array containing the specified elements.

Examples

1. Using booleanArrayOf() to Create a Boolean Array

In this example, we’ll use booleanArrayOf() to create a boolean array with specified values.

Kotlin Program

fun main() {
    val booleanArray = booleanArrayOf(true, false, true, false)
    
    // Display the created boolean array
    println("Created Boolean Array: ${booleanArray.joinToString(", ")}")
}

Output

Created Boolean Array: true, false, true, false

In this example, booleanArrayOf() is used to create a boolean array with specified values, and the array is displayed using joinToString().

Summary

The booleanArrayOf() function in Kotlin is a convenient way to initialize a boolean array with specified values. It simplifies the process of creating boolean arrays with a concise syntax.