
- Kotlin - Home
- Kotlin - Overview
- Kotlin - Environment Setup
- Kotlin - Architecture
- Kotlin - Basic Syntax
- Kotlin - Comments
- Kotlin - Keywords
- Kotlin - Variables
- Kotlin - Data Types
- Kotlin - Operators
- Kotlin - Booleans
- Kotlin - Strings
- Kotlin - Arrays
- Kotlin - Ranges
- Kotlin - Functions
- Kotlin Control Flow
- Kotlin - Control Flow
- Kotlin - if...Else Expression
- Kotlin - When Expression
- Kotlin - For Loop
- Kotlin - While Loop
- Kotlin - Break and Continue
- Kotlin Collections
- Kotlin - Collections
- Kotlin - Lists
- Kotlin - Sets
- Kotlin - Maps
- Kotlin Objects and Classes
- Kotlin - Class and Objects
- Kotlin - Constructors
- Kotlin - Inheritance
- Kotlin - Abstract Classes
- Kotlin - Interface
- Kotlin - Visibility Control
- Kotlin - Extension
- Kotlin - Data Classes
- Kotlin - Sealed Class
- Kotlin - Generics
- Kotlin - Delegation
- Kotlin - Destructuring Declarations
- Kotlin - Exception Handling
Kotlin List - get() Function
The Kotlin List get() function is used to display the element from the list at the specified index. It throws an exception " IndexOutOfBoundsException" if the specified index is not in the range.
Get the more about the get() function for array, visit here
Syntax
Following is the syntax of the Kotlin list get() function −
fun <K, V> Map<K, V>.get(key: K): V?
Parameters
This function accepts an integer as parameter, which represents the index value of a list.
Return value
This function returns an element from the list.
Example 1: Get the Index Value
Let's see a basic example of the get() function, which return element of the given index −
fun main(args: Array<String>) { val fruits = listOf("Apple", "Banana", "Cherry", "Date") // Get the element at index 2 val fruit = fruits.get(2) println("Fruit at index 2: $fruit") // Equivalent using square brackets println("Fruit at index 3: ${fruits[3]}") }
Output
Following is the output −
Fruit at index 2: Cherry Fruit at index 3: Date
Example 2: Invalid Index
In the following example, we pass the invalid index tothe get()function. If the index is invalid, then the function returns an exception −
fun main(args: Array<String>) { val fruits = listOf("Apple", "Banana", "Cherry", "Date") // Attempting to access an invalid index try { val invalidFruit = fruits.get(5) } catch (e: IndexOutOfBoundsException) { println("Error: ${e.message}") } }
Output
Following is the output −
Error: Index 5 out of bounds for length 4
Example 3: Accessing Every Nth Element Dynamically
In this example, we have a list of names and iterate through each second element of the list to display every second element of the list dynamically, we also take the user index to display the element −
fun main(args: Array<String>) { val names = listOf("Aman", "Kumar", "Vivek", "Dipak", "Rohan", "Sohan") // Access every 2nd element for (i in names.indices step 2) { println("Element at index $i: ${names.get(i)}") } // Access an element using a user-provided index val userIndex = 3 if (userIndex in names.indices) { println("Element at user-provided index $userIndex: ${names.get(userIndex)}") } else { println("Index $userIndex is out of bounds!") } }
Output
Following is the output −
Element at index 0: Aman Element at index 2: Vivek Element at index 4: Rohan Element at user-provided index 3: Dipak