Determine if an index exists in an array in Kotlin
This article explores different ways to determine if an index exists in an array in Kotlin.
Whenever we try to access an array with a negative index or index more than or equal to the array’s length, the java.lang.ArrayIndexOutOfBoundsException is thrown. For instance,
|
1 2 3 4 5 6 |
fun main() { val array: IntArray = intArrayOf(5, 7, 3, 4, 9, 1, 1) val index = 7 println("The value present at the index $index is ${array[index]}") } |
Output:
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: Index 7 out of bounds for length 6
at MainKt.main(Main.kt:5)
at MainKt.main(Main.kt)
We can avoid getting an index out of bounds exception by explicitly checking if the array index is within the bounds of the array before accessing it.
1. Using In operator
The idiomatic way to check if an index exists in an array is using the in operator, which provides concise and readable syntax.
|
1 2 3 4 5 6 7 8 9 10 |
fun main() { val array: IntArray = intArrayOf(5, 7, 3, 4, 9, 1) val index = 7 if (index in 0..array.lastIndex) { println("The value present at the index $index is ${array[index]}") } else { println("Index $index out of bounds for length ${array.size}") } } |
Output:
Index 7 out of bounds for length 6
The above code can be further shortened using .indices, which returns the range of valid indices for the array.
|
1 2 3 4 5 6 7 8 9 10 |
fun main() { val array: IntArray = intArrayOf(5, 7, 3, 4, 9, 1) val index = 7 if (index in array.indices) { println("The value present at the index $index is ${array[index]}") } else { println("Index $index out of bounds for length ${array.size}") } } |
Output:
Index 7 out of bounds for length 6
Note that in operator is equivalent to calling the contains() function. The expression x in y is translated to y.contains(x).
|
1 2 3 4 5 6 7 8 9 10 |
fun main() { val array: IntArray = intArrayOf(5, 7, 3, 4, 9, 1) val index = 7 if (array.indices.contains(index)) { println("The value present at the index $index is ${array[index]}") } else { println("Index $index out of bounds for length ${array.size}") } } |
Output:
Index 7 out of bounds for length 6
2. Using && operator
The array index is valid if it is non-negative and less than the array’s length. We can easily determine if an index is valid using a conditional expression, as follows:
|
1 2 3 4 5 6 7 8 9 10 |
fun main() { val array: IntArray = intArrayOf(5, 7, 3, 4, 9, 1) val index = 7 if (index >= 0 && index < array.size) { println("The value present at the index $index is ${array[index]}") } else { println("Index $index out of bounds for length ${array.size}") } } |
Output:
Index 7 out of bounds for length 6
3. Using getOrNull() function
The getOrNull() function returns an element at the given index, or null if the index is out of bounds of the array. It can be used as follows to determine if an index is valid or not.
|
1 2 3 4 5 6 7 8 9 10 |
fun main() { val array: IntArray = intArrayOf(5, 7, 3, 4, 9, 1) val index = 7 if (array.getOrNull(index) != null) { println("The value present at the index $index is ${array[index]}") } else { println("Index $index out of bounds for length ${array.size}") } } |
Output:
Index 7 out of bounds for length 6
Additionally, the value at the given index can be determined using the let function:
|
1 2 3 4 5 6 7 8 |
fun main() { val array: IntArray = intArrayOf(5, 7, 3, 4, 9, 1) val index = 4 array.getOrNull(index)?.let { println("The value present at the index $index is $it") } } |
Output:
The value present at the index 4 is 9
4. Using try-catch block
Finally, we can just try accessing the array using the indexing operator within a try-catch block. It throws IndexOutOfBoundsException if the index is out of bounds. It can be used as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
fun isValidIndex(array: IntArray, index: Int): Boolean { try { array[index] } catch (e: IndexOutOfBoundsException) { return false } return true } fun main() { val array: IntArray = intArrayOf(5, 7, 3, 4, 9, 1) val index = 7 if (isValidIndex(array, index)) { println("The value present at the index $index is ${array[index]}") } else { println("Index $index out of bounds for length ${array.size}") } } |
Output:
Index 7 out of bounds for length 6
That’s all about determining if an index exists in an array in Kotlin.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)