Kotlin .indices

Sriparno08's avatar
Published Nov 23, 2024
Contribute to Docs

In Kotlin, the .indices property returns a range indicating the valid indices in an array.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn Kotlin, the expressive, open-source programming language developed by JetBrains.
    • Beginner Friendly.
      9 hours

Syntax

arr.indices
  • arr: The array whose valid indices are to be accessed.

Example

The following example demonstrates the usage of the .indices property:

fun main() {
// Creating an array
val arr = intArrayOf(1, 6, 12, 18, 24, 30)
// Using the '.indices' property
val num = arr.indices
// Printing the result
println("$num")
}

The above code produces the following output:

0..5

All contributors

Contribute to Docs

Learn Kotlin on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn Kotlin, the expressive, open-source programming language developed by JetBrains.
    • Beginner Friendly.
      9 hours