Scala Stack splitAt() 方法与示例
在 Scala Stack class
中, splitAt()方法用于将给定的堆栈拆分为指定位置的一对前缀/后缀堆栈。
Method Definition: def splitAt(n: Int): (Stack[A], Stack[A])
Return Type: It returns a pair of stacks consisting of the first n elements of this stack, and the other elements.
示例 #1:
// Scala program of splitAt()
// method
// Import Stack
import scala.collection.mutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating stack
val s1 = Stack(5, 2, 13, 7, 1)
// Print the stack
println(s1)
// Applying splitAt method
val result = s1.splitAt(2)
// Display output
print(result)
}
}
输出:
Stack(5, 2, 13, 7, 1)
(Stack(5, 2), Stack(13, 7, 1))
示例 #2:
// Scala program of splitAt()
// method
// Import Stack
import scala.collection.mutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating stack
val s1 = Stack(5, 2, 13, 7, 1)
// Print the stack
println(s1)
// Applying splitAt method
val result = s1.splitAt(3)
// Display output
print(result)
}
}
输出:
Stack(5, 2, 13, 7, 1)
(Stack(5, 2, 13), Stack(7, 1))