Kotlin .get()

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

In Kotlin, the .get() function returns the element located at a given position 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.get(pos)
  • arr: The array from which an element is retrieved.
  • pos: The position (index) in the array where the target element is located.

Example

The following example demonstrates the usage of the .get() function:

fun main() {
// Creating an array
val arr = intArrayOf(1, 8, 16, 24, 32, 40)
// Accessing the element located at index '3' in the array
val elm = arr.get(3)
// Printing the fetched element
println("$elm")
}

The above code produces the following output:

24

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