So far we have seen variables, which is used to store a single value. But what if we have to store 100 values?
We cannot just create 100 variables.
And to fill this gap arrays came into picture.
An 'Array' is a Data Structure that holds multiple values, of different Data Type.
The declaration of a 'Array' is super easy.
Below is the way of declaring an Int array of size 5:
The syntax is quite simple.
To create an Array, you need to tell Kotlin that the variable 'arr' is going to hold an Array.
So you need to specify 'Array' followed by 'Int' in angular brackets '<>' (To specify that the array will hold integers) then specify the size of the Array inside brackets '()' (It holds the array of size '5'). And lastly specify the default value inside braces '{}'
So, in the next example, we will be asking Kotlin to store the numbers 1 to 5 in an array.
fun main() { var arr = Array<Int>(5){0} for (i in 0..4) { arr[i] = i + 1 } for (i in 0..4) { println(arr[i]) } }
So, in the above code, we have declared an Array 'arr' that is going to store the values from 1 to 5.
As we have seen above, the first line declares an array, 'arr' that would store 5 numbers.
Now, internally what happens is, 5 empty locations are created with indexes from '0' to '4' and all are initialised with '0'.
Then we have used the for loop to insert the numbers from 1 to 5 in each Iteration
for (i in 0..4) { arr[i] = i + 1 }
So, in the first Iteration of for loop, the value of 'i' is '0'.
And we use this 'i' as index to access the first location of the array.
Accessing array elements is quite simple. Just put the index (i.e. 'i') inside square brackets, '[]' and you can access that element.
And what happens is, the value from the right hand side (i.e. 'i+1' or '0+1' or '1') gets assigned to the first array location (i.e. arr[0]).
So, internally the number '1' goes and sits inside 'arr[0]'.
Similarly, in the second Iteration of for loop, the value of 'i' becomes '1'.
And in the same way, we use this 'i' as index to access the second location of the array.
And as we have seen, the value from the right hand side (i.e. 'i+1' or '1+1' or '2') gets assigned to the second array location (i.e. arr[1]).
So, internally the number '2' goes and sits inside 'arr[1]'.
Continuing in the same way, all the numbers are assigned to all the 5 locations in the Array.
Now that we have all the 5 numbers stored in the Array. We can use another 'for loop' to display all the numbers.
for (i in 0..4) { println(arr[i]) }
The above 'for loop' picks all the numbers one by one and displays on the screen.
Declaring an Array that is going to hold 5 String/names is quite similar as creating an Array that holds numbers.
Just replace 'Int' with 'String' and the default value to '""'.
So, in the next example, we will be asking Kotlin to store five names, 'Mohan', 'John', 'Paul', 'Kriti' and 'Salim' in the Array.
fun main() { var arr = Array<String>(5){""} arr[0] = "Mohan" arr[1] = "John" arr[2] = "Paul" arr[3] = "Kriti" arr[4] = "Salim" for (i in 0..4) { println(arr[i]) } }
So, in the above code, we have declared an Array 'arr' that is going to store five names,'Mohan', 'John', 'Paul', 'Kriti' and 'Salim'
And we have created a String Array with a default value i.e. '""'.
Now, internally what happens is, 5 locations are created to hold values of String type with indexes from '0' to '4'.
And in the next few lines, we have assigned the names to the respective location, one by one.
Then we have used the for loop to to display the names in each Iteration
for (i in 0..4) { println(arr[i]) }
And we get the below output.
In the next tutorial, we will see, how we can create an array with default values using arrayOf() function.