Scala Queue ++:()方法及示例
利用 ++:() 方法可以返回一个新的队列,并将该队列的所有元素附加在另一个队列的前面。
方法定义: def ++:[B >: A](prefix: IterableOnce[B]):Queue[B]
返回类型。它返回一个新的队列,该队列的所有元素被附加在另一个队列的前面。
例子 #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)
val q2 = Queue(11, 12, 13, 14, 15)
// Applying ++:() method
val result = q1.++:(q2)
// Display output
print(result)
}
}
输出。
Queue(11, 12, 13, 14, 15, 1, 2, 3, 4, 5)
例子#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("for", "geeks")
val q2 = Queue("geeks")
// Applying ++:() method
val result = q1.++:(q2)
// Display output
print(result)
}
}
输出。
Queue(geeks, for, geeks)
极客教程