Kotlin List toBooleanArray()
Syntax & Examples
Syntax of List.toBooleanArray()
The syntax of List.toBooleanArray() extension function is:
fun Collection<Boolean>.toBooleanArray(): BooleanArrayThis toBooleanArray() extension function of List returns an array of Boolean containing all of the elements of this collection.
✐ Example
1 Example
In this example,
- We create a list named
listcontaining the Boolean valuestrue, false, true, false. - We call the
toBooleanArray()function onlist, which converts the list to a Boolean array. - The resulting Boolean array is stored in
result. - Finally, we print the content of
resultto standard output using thecontentToString()function.
Kotlin Program
fun main(args: Array<String>) {
val list = listOf(true, false, true, false);
val result = list.toBooleanArray();
println(result.contentToString());
}Output
[true, false, true, false]
Summary
In this Kotlin tutorial, we learned about toBooleanArray() extension function of List: the syntax and few working examples with output and detailed explanation for each example.