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




KOTLIN - LISTS


A 'List' is a Collection that holds multiple values, of different Data Types. And in a 'List' the elements are ordered (We will explain it soon) and allows duplicate values.


The declaration of a 'List' 'Data Type' is super easy. You can place multiple values inside the method 'listOf()' or 'mutableListOf()' and Kotlin will understand that it is a 'List'.


Creating a read only 'List' with values of different Data Types using listOf()


Using the 'listOf()' method, we can create an immutable list(i.e. A read only List).


Example :



fun main() {
    var x = listOf(5, "John", "Kotlin")
    println(x)
}


Output :



 [5, 'John', 'Kotlin']

So, in the above code we have created a 'List' using the 'listOf()' method.


And put an Integer type value (i.e. 5) and two String type value (i.e. 'John' and 'Kotlin')


var x = listOf(5, "John", "Kotlin")

And initialised to the variable 'x'.


java_Collections

So, we can see that two different data types are assigned to a 'List'.


In the next line we have printed the 'List' using the print statement.


println(x)

Now, if we see the output,



Output :



 [5, 'John', 'Kotlin']

The values are enclosed inside square brackets '[]'. This means that the values are inside a List.


So, in the above code we have created a List with 2 different Datatype(i.e. String and Integer).


Now, what if we want to restrict the List to accept only a String or an Integer or a Float.


Let's see in the below example.


Restricting the List to accept elements of one Datatype


Example :



fun main() {
    var x: List<String> = listOf(5, "John", "Kotlin")
    println(x)
} 


Output :



 Type inference failed. Expected type mismatch: inferred type is List<Any> but List<String> was expected

So, what we have done is, created a List with three values, '5', 'John' and 'Kotlin'.


var x: List<String> = listOf(5, "John", "Kotlin")

And at the time of initialisation, we have ended up with an error.


Type inference failed. Expected type mismatch: inferred type is List<Any> but List<String> was expected

This is because we have restricted the List to accept only values of String datatype.


java_Collections

And the values we are trying to use contains an integer value i.e. '5'.


java_Collections

Now, let us try inserting all String values.


Example :



fun main() {
    var x: List<String> = listOf("Tom", "John", "Kotlin")
    println(x)
}


Output :



 [Tom, John, Kotlin]

Now, if you look at the output. We got the correct entries printed.


So, the values are enclosed inside square brackets '[]'. This means that the values are inside a List.


Now, what if we don't want the output enclosed in square brackets '[]'.


Let us see that in the below example.


Iterating a List using 'for loop'


Example :



fun main() {
    var x = listOf(5, "John", "Kotlin")
        
    for (i in x) {
        println(i)
    }
}


Output :



 5
 John
 Kotlin

Similarly, in the above code we have created a 'List' using the 'listOf()' method.


var x = listOf(5, "John", "Kotlin")

And initialised to the variable 'x'.


java_Collections

In the next line we have used the 'for loop' to Iterate through the 'List'.


for i in x:
    print(i)

Now, if we see the iterations of for loop,


for (i in x) {
    println(i)
}

1st Iteration


In the first Iteration the first value of the 'List' 'x' (i.e. 5) is taken and put into the variable 'i'.


java_Collections

And the print statement, prints the value of 'i'.


Output :



 5

2nd Iteration


Similarly, in the second Iteration the second value of the 'List' 'x' (i.e. 'John') is taken and put into the variable 'i'.


java_Collections

And the print statement, prints the value of 'i'.


Output :



 5
 John

3rd Iteration


Similarly, in the third Iteration the third value of the 'List' 'x' (i.e. 'Kotlin') is taken and put into the variable 'i'.


And the print statement, prints the value of 'i'.


Output :



 5
 John
 Kotlin

Now, if you see the final output. You can find that the values of the 'List' are displayed in the same way they were inserted.


i.e. First '5' is printed, then the name 'John' and finally 'Kotlin' is printed.


And if you see the List,


x = [5, "John", "Kotlin"]

It is printed in the same order it was inserted. And this is why a List is said to be 'Ordered'.


Creating a mutable 'List' using mutableListOf() method


Using the 'mutableListOf()' method, we can create a mutable list(i.e. The List values can be changed/replaced).


Example :



fun main() {
    var x = mutableListOf(5, "John", "Kotlin")
    println(x)
}


Output :



 [5, 'John', 'Kotlin']

Next, let us see, how to access the elements of the List in the next tutorial.