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:
import scala.collection.mutable. _
object GfG
{
def main(args : Array[String])
{
val q 1 = Queue( 10 , 11 , 12 , 13 , 14 )
println(q 1 )
val result = q 1 .apply( 0 )
print( "Element at index 0 : " + result)
}
}
|
Output:
Queue(10, 11, 12, 13, 14)
Element at index 0 : 10
Example #2:
import scala.collection.mutable. _
object GfG
{
def main(args : Array[String])
{
val q 1 = Queue( 10 , 11 , 12 , 13 , 14 )
println(q 1 )
val result = q 1 .apply( 2 )
print( "Element at index 2 : " + result)
}
}
|
Output:
Queue(10, 11, 12, 13, 14)
Element at index 2 : 12
Share your thoughts in the comments
Please Login to comment...