Related Articles

Related Articles

Scala Queue apply() method with example
  • Last Updated : 18 Oct, 2019

The apply() method is utilized to find the element at any given index in a queue.

Method Definition: def apply(idx: Int): A

Return Type: It returns the element at the given index of the queue.

Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of apply() 
// method 
  
// Import Queue  
import scala.collection.mutable._
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
        // Creating a queue 
        val q1 = Queue(10, 11, 12, 13, 14)
        
        // Print the queue
        println(q1)
          
        // Applying apply() method 
        val result = q1.apply(0)
          
        // Display output
        print("Element at index 0 : " + result)   
          
    

chevron_right


Output:

Queue(10, 11, 12, 13, 14)
Element at index 0 : 10

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of apply() 
// method 
  
// Import Queue  
import scala.collection.mutable._
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
        // Creating a queue 
        val q1 = Queue(10, 11, 12, 13, 14)
          
        // Print the queue
        println(q1)
  
        // Applying apply() method 
        val result = q1.apply(2)
          
        // Display output
        print("Element at index 2 : " + result)   
          
    

chevron_right


Output:

Queue(10, 11, 12, 13, 14)
Element at index 2 : 12



My Personal Notes arrow_drop_up
Recommended Articles
Page :