Kotlin Array.none()

Kotlin Array.none() Tutorial

In Kotlin, the Array.none() function is used to check if none of the elements of the given array satisfy a given predicate. It returns true if no elements match the predicate, otherwise false.

If no predicate is given, then none() function returns true if the array is empty, and false if it contains any element.

In this tutorial, we’ll explore the syntax of the Array.none() function and provide examples of its usage in Kotlin, both with and without a predicate.

Syntax

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

fun <T> Array<out T>.none(): Boolean
fun <T> Array<out T>.none(predicate: (T) -> Boolean): Boolean

where

ParameterDescription
predicateFunction that takes the element as argument and returns True or False based on user custom logic.
Parameters of Array.none()

The Array.none() function returns a Boolean value.

Examples for Array none() function

1. Using none() with a Predicate

In this example, we’ll use none() with a predicate to check if none of the numbers in an array are negative.

Kotlin Program

fun main() {
    val numbersArray = arrayOf(1, 2, 3, 4, 5)
    println("Numbers Array:\n${numbersArray.joinToString(", ")}\n")

    if (numbersArray.none { it < 0 }) {
        print("None of the numbers in given array are negative.")
    } else {
        print("There are some numbers in given array that are negative.")
    }
}

Output

Numbers Array:
1, 2, 3, 4, 5

None of the numbers in given array are negative.

Now, let us take some negative numbers in the array numbersArray and run the program.

Kotlin Program

fun main() {
    val numbersArray = arrayOf(-8, 2, 3, -7, 10)
    println("Numbers Array:\n${numbersArray.joinToString(", ")}\n")

    if (numbersArray.none { it < 0 }) {
        print("None of the numbers in given array are negative.")
    } else {
        print("There are some numbers in given array that are negative.")
    }
}

Output

Numbers Array:
-8, 2, 3, -7, 10

There are some numbers in given array that are negative.

2. Using none() without a Predicate

In this example, we’ll use none() without a predicate to check if none of the elements in an array are negative.

Kotlin Program

fun main() {
    val numbersArray = arrayOf<Int>()
    println("Numbers Array:\n${numbersArray.contentToString()}")

    if (numbersArray.none()) {
        print("The given array is empty.")
    } else {
        print("The given array is not empty.")
    }
}

Output

Numbers Array:
[]
The given array is empty.

Now, let us take some elements in the array numbersArray and run the program.

Kotlin Program

fun main() {
    val numbersArray = arrayOf<Int>(10, 20, 30)
    println("Numbers Array:\n${numbersArray.contentToString()}")

    if (numbersArray.none()) {
        print("The given array is empty.")
    } else {
        print("The given array is not empty.")
    }
}

Output

Numbers Array:
[10, 20, 30]
The given array is not empty.

Summary

In this tutorial, we’ve covered the Array.none() function in Kotlin, its syntax, and how to use it to check if none of the elements satisfy a given predicate. This function is useful for scenarios where you want to ensure that no element in the array matches a specific condition.