The indices extension property for sets in Kotlin returns an IntRange of the valid indices for the set. Note that since sets are unordered collections, the indices property is more conceptual and mainly used for consistent API design.
The syntax of Set.indices extension property is:
val Set<*>.indices: IntRange
This indices extension property of Set returns an IntRange of the valid indices for this set.
Set.indices returns value of type IntRange
.
In Kotlin, we can use the indices
extension property to get the range of valid indices for a set.
For example,
indices
property to get the range of valid indices.println
function.fun main(args: Array<String>) {
val numbers = setOf(1, 2, 3, 4, 5)
val indices = numbers.indices
println("The valid indices are: $indices")
}
The valid indices are: 0..4
In Kotlin, we can use the indices
property to get the valid indices of a non-empty set.
For example,
indices
property to get the range of valid indices.println
function.fun main(args: Array<String>) {
val fruits = setOf("apple", "banana", "cherry")
val indices = fruits.indices
println("The valid indices are: $indices")
}
The valid indices are: 0..2
In Kotlin, we can use the indices
property to get the valid indices of an empty set, which will result in an empty range.
For example,
indices
property to get the range of valid indices.println
function.fun main(args: Array<String>) {
val emptySet = emptySet<Int>()
val indices = emptySet.indices
println("The valid indices are: $indices")
}
The valid indices are: 0..-1
In this Kotlin tutorial, we learned about indices extension property of Set: the syntax and few working examples with output and detailed explanation for each example.