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




ELEMENTAT() - FUNCTION


elementAt() method is used for accessing the elements of the Set


We have the Set with three values, 5, John and Kotlin. And we want to access the second element i.e. John using the elementAt() method.


Example :



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


Output :



  John

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


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

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

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/index 1 using the elementAt() method (i.e. x.elementAt(1)).


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


println(x.elementAt(1))