Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




KOTLIN - ARRAYS


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.


Declaring an empty Array to hold numbers


Below is the way of declaring an Int array of size 5:


var arr = Array<Int>(5){0}

The syntax is quite simple.


To create an Array, you need to tell Kotlin that the variable 'arr' is going to hold an Array.


var arr = Array<Int>(5){0}

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 '{}'


java_Collections

So, in the next example, we will be asking Kotlin to store the numbers 1 to 5 in an array.


Example :



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])
    }
}


Output :



 1
 2
 3
 4
 5

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.


var arr = Array<Int>(5){0}

Now, internally what happens is, 5 empty locations are created with indexes from '0' to '4' and all are initialised with '0'.


java_Collections

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
}

1st Iteration


So, in the first Iteration of for loop, the value of 'i' is '0'.


java_Collections

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.


arr[i] = i + 1

java_Collections

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]'.


java_Collections

2nd Iteration


Similarly, in the second Iteration of for loop, the value of 'i' becomes '1'.


java_Collections

And in the same way, we use this 'i' as index to access the second location of the array.


arr[i] = i + 1

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]'.


java_Collections

Continuing in the same way, all the numbers are assigned to all the 5 locations in the Array.


java_Collections

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.


Output :



 1
 2
 3
 4
 5

Declaring an Array to hold Strings


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 '""'.


var arr = Array<String>(5){""}

So, in the next example, we will be asking Kotlin to store five names, 'Mohan', 'John', 'Paul', 'Kriti' and 'Salim' in the Array.


Example :



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])
    }
}


Output :



 Mohan
 John
 Paul
 Kriti
 Salim

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. '""'.


var arr = Array<String>(5){""}

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.


arr[0] = "Mohan"
arr[1] = "John"
arr[2] = "Paul"
arr[3] = "Kriti"
arr[4] = "Salim"

java_Collections

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.


Output :



 Mohan
 John
 Paul
 Kriti
 Salim

In the next tutorial, we will see, how we can create an array with default values using arrayOf() function.