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




KOTLIN - ACCESSING LIST


Accessing the elements of the 'List' using Indexes


We have the 'List' with three values, 5, 'John' and 'Kotlin'. And we want to access the second element i.e. 'John'.


Example :



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


Output :



 John

So, in the above code we have created a 'List' and initialised to the variable 'x'.


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

Now, let us see, how the values are positioned in the List


java_Collections

So, as we can see the elements are positioned as '0', '1' and '2'. And if we want to access the second element, we can refer to the position '1' using the square brackets (i.e. x[1]).


And the print statement prints the value of the second element of the 'List' (i.e. 'John').


println(x[1])

Accessing the elements of the 'List' using get() method


Let us take the same example as above. And access the second element i.e. 'John' using the 'get()' method.


Example :



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


Output :



 John

So, in the above code we have created a 'List' and initialised to the variable 'x'.


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

Now, let us see, how the values are positioned in the List,


java_Collections

So, as we can see the elements are positioned as '0', '1' and '2'. And if we want to access the second element, we can refer to the position '1' using the 'get()' method (i.e. 'x.get(1)').


And the print statement prints the value of the second element of the 'List' (i.e. 'John').


println(x.get(1))

Accessing a chunk of elements of the 'List' using 'subList()' method


We have the 'List' with five values, 'Mohan', 'John', 'Paul', 'Kriti' and 'Salim'. And we want to access the second, third and fourth element i.e. 'John', 'Paul' and 'Kriti'.


Example :



fun main() {
    var x = listOf("Mohan", "John", "Paul", "Kriti", "Salim")
    println(x.subList(1,4))
} 


Output :



 [John, Paul, Kriti]

So, in the above code we have created a 'List' and initialised to the variable 'x'.


var x = listOf("Mohan", "John", "Paul", "Kriti", "Salim")

Now, let us see, how the values are positioned in the List


java_Collections

So, as we can see the elements are positioned as '0', '1', '2', '3' and '4'.


Now, if we want to access the second, third and fourth element (i.e. 'John', 'Paul' and 'Kriti').


We can specify the range using 'subList()' method.


i.e. x.subList(1,4)


'x.subList(1,4)' actually tells, to pick the elements from index/position '1' to the index/position '4-1' i.e. 3.


java_Collections

And the print statement prints the second, third and fourth element (i.e. 'John', 'Paul' and 'Kriti').


println(x.subList(1,4))

Output :



 ['John', 'Paul', 'Kriti']

So, if you look at the above output, the values are enclosed within square brackets '[]'. That is because a new array is created with the three elements 'John', 'Paul' and 'Kriti'.


Note : The 'subList()' method creates a new array with the new values.

Accessing a chunk of elements of the 'List' using 'slice()' method


Let us take the above example, where we have the 'List' with five values, 'Mohan', 'John','Paul', 'Kriti' and 'Salim'. And we want to access the second, third and fourth element i.e. 'John', 'Paul' and 'Kriti'.


Example :



fun main() {
    var x = listOf("Mohan", "John", "Paul", "Kriti", "Salim")
    println(x.slice(1..3))
}


Output :



 [John, Paul, Kriti]

So, in the above code we have created a 'List' and initialised to the variable 'x'.


var x = listOf("Mohan", "John", "Paul", "Kriti", "Salim")

Now, let us see, how the values are positioned in the List


java_Collections

So, as we can see the elements are positioned as '0', '1', '2', '3' and '4'.


Now, if we want to access the second, third and fourth element (i.e. 'John', 'Paul' and 'Kriti').


We can specify the range using 'slice()' method.


i.e. x.slice(1..3)


'x.slice(1..3)' actually tells, to pick the elements from index/position '1' to the index/position 3.


java_Collections

And the print statement prints the second, third and fourth element (i.e. 'John', 'Paul' and 'Kriti').


println(x.slice(1..3))

Output :



 ['John', 'Paul', 'Kriti']

So, if you look at the above output, the values are enclosed within square brackets '[]'. That is because a new array is created with the three elements 'John', 'Paul' and 'Kriti'.


Note : The 'slice()' method creates a new array with the new values.