Scala Queue splitAt()方法及示例
splitAt()方法是用来将给定的队列在规定的位置分割成一对前缀/后缀的队列。
方法定义: def splitAt(n: Int):(Queue[A], Queue[A])
其中,n是我们需要分割的位置。
返回类型。它返回一对队列,由这个队列的前n个元素和其他元素组成。
例子 #1:
// Scala program of splitAt()
// method
// Import Queue
import scala.collection.mutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a queue
val q1 = Queue(5, 2, 13, 7, 1)
// Print the queue
println(q1)
// Applying splitAt method
val result = q1.splitAt(3)
// Display output
print(result)
}
}
输出。
Queue(5, 2, 13, 7, 1)
(Queue(5, 2, 13), Queue(7, 1))
例子#2。
// Scala program of splitAt()
// method
// Import Queue
import scala.collection.mutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a queue
val q1 = Queue(5, 2, 13, 7, 1)
// Print the queue
println(q1)
// Applying splitAt method
val result = q1.splitAt(2)
// Display output
print(result)
}
}
输出。
Queue(5, 2, 13, 7, 1)
(Queue(5, 2), Queue(13, 7, 1))
极客教程