Scala Queue apply()方法及示例

Scala Queue apply()方法及示例

apply() 方法是用来寻找队列中任何给定索引的元素的。

方法定义: def apply(idx: Int):A

返回类型。它返回队列中给定索引处的元素。

例子 #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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程