October 2018
Intermediate to advanced
370 pages
9h 15m
English
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 0, intArr[0], to access the first element of array and index 4, intArr[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 = 0Read now
Unlock full access