Scala Queue apply() 方法与示例
apply()方法用于在队列中的任何给定索引处查找元素。
Method Definition: def apply(idx: Int): A
Return Type: It returns the element at the given index of the queue.
示例 #1:
// 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)
}
}
输出:
Queue(10, 11, 12, 13, 14)
Element at index 0 : 10
示例 #2:
// 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)
}
}
输出:
Queue(10, 11, 12, 13, 14)
Element at index 2 : 12