當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Scala Queue :+()用法及代碼示例

:+() 方法用於返回一個新隊列,並在給定隊列的後麵添加一個元素。

函數定義: def:+[B >: A](elem: B): Queue[B]

返回類型: It returns a new queue with an element added at the back of the given queue.

示例#1:


// Scala program of :+()  
// method  
  
// Import Queue   
import scala.collection.mutable._
  
// Creating object  
object GfG  
{  
  
    // Main method  
    def main(args:Array[String])  
    {  
        // Creating a queue  
        val q1 = Queue(1, 2, 3, 4, 5) 
          
        // Applying :+() method  
        val res = q1.:+(10) 
          
        // Display output 
        print(res)    
          
    }  
}  
輸出:
Queue(1, 2, 3, 4, 5, 10)

示例#2:


// Scala program of :+()  
// method  
  
// Import Queue   
import scala.collection.mutable._
  
// Creating object  
object GfG  
{  
  
    // Main method  
    def main(args:Array[String])  
    {  
        // Creating a queue  
        val q1 = Queue("geeks", "for") 
          
        // Applying :+() method  
        val res = q1.:+("geeks") 
          
        // Display output 
        print(res)    
          
    }  
}  
輸出:
Queue(geeks, for, geeks)


相關用法


注:本文由純淨天空篩選整理自rupesh_rao大神的英文原創作品 Scala Queue :+() method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。