Scala Queue +:() 方法示例
+:()方法用于返回一个新队列,其中在给定队列的前面添加了一个元素。
Method Definition: def +:[B >: A](elem: B): Queue[B]
Return Type: It returns a new queue with an element added at the front 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("for", "geeks")
// Applying +:() method
val result = q1.+:("geeks")
// Display output
print(result)
}
}
输出:
Queue(geeks, for, geeks)
示例 #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(2, 3, 4, 5, 6)
// Applying +:() method
val result = q1.+:(1)
// Display output
print(result)
}
}
输出:
Queue(1, 2, 3, 4, 5, 6)