Scala List apply() method with example
Last Updated :
26 Jul, 2019
The apply() method is utilized to select an element in the list by its index.
Method Definition: def apply(n: Int): A
Return Type: It returns an element from the given list by its index which is present in the apply method as argument.
Example #1:
object GfG
{
def main(args : Array[String])
{
val m 1 = List( 1 , 3 , 5 , 2 )
val res = m 1 .apply( 3 )
println(res)
}
}
|
Example #2:
object GfG
{
def main(args : Array[String])
{
val m 1 = List( 1 , 3 , 5 , 2 )
val res = m 1 .apply( 0 )
println(res)
}
}
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...