The Wayback Machine - https://web.archive.org/web/20250118111234/https://www.geeksforgeeks.org/scala-list-apply-method-with-example/
Open In App

Scala List apply() method with example

Last Updated : 26 Jul, 2019
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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:




// Scala program of apply()
// method
  
// Creating object
object GfG
{ 
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating a list
        val m1 = List(1, 3, 5, 2)
          
        // Applying apply method
        val res = m1.apply(3)
          
        // Displays output
        println(res)
      
    }
}


Output:

2

Example #2:




// Scala program of apply()
// method
  
// Creating object
object GfG
{ 
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating a list
        val m1 = List(1, 3, 5, 2)
          
        // Applying apply method
        val res = m1.apply(0)
          
        // Displays output
        println(res)
      
    }
}


Output:

1

Get 90% Course fee refund on completing 90% course in 90 days! Take the Three 90 Challenge today.

The next 90 Days of focus & determination can unlock your full potential. The Three 90 challenge has started and this is your chance to upskill and get 90% refund. What more motivation do you need? Start the challenge right away!


Similar Reads

three90RightbarBannerImg