Scala Stack :+() 方法与示例
在 Scala 中,scala.collection.mutable 实现了 Stack 数据结构。 :+ 方法用于创建此堆栈的副本,并附加一个元素。
Method Definition – def :+(elem: A)
Returns – a new stack consisting of all elements of this stack followed by elem.
示例 #1:
// Scala program of mutable stack :+() method
// Import Stack
import scala.collection.mutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a stack
val q2 = List(11, 12, 13, 14, 15)
// Applying :+() method
val result = q2 :+ 100
// Display output
print(result)
}
}
输出:
List(11, 12, 13, 14, 15, 100)
示例 #2:
// Scala program of mutable stack /:()
// method
// Import Stack
import scala.collection.mutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
val st1 = Stack(1, 2, 3)
val st2 = Stack("Geeks", "For")
// Applying :+() method
val result1 = st2 :+ st1
// Display output
print(result1)
}
}
输出:
Stack(Geeks, For, Stack(1, 2, 3))