Index of the array element

There are a number of ways to access the array elements. One method is to use the subscript operator, []. Take a look at the following example:

val intArr = arrayOf(1,2,3,4,5)
var element = intArr[0]

An individual element can be referenced using the array name along with the [index] brackets, which contain the index. As we know, an index always starts from 0. If we create an array of five elements, we can use index 0intArr[0], to access the first element of array and index 4intArr[4], to access the last element of the array. Create an array of integer type and loop through it using a traditional while loop:

fun readyArrayByIndex(){
val intArr = intArrayOf(1,2,3,4,5)
val arraySize = intArr.size

var index = 0
while(index < arraySize){
println("At index $index Value ${intArr[index]}")
index++
}
}

Create a variable called index and initialize it with 0. In the while loop, assign the index variable to intArr[index] and increase the index value on each iteration.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.205.136