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




LIST - get()


get() is used to access any element in the List using its index.


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.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))