Scala Stack push()方法与实例
在Scala Stack类中, push() 方法被用来在栈顶添加一个元素。
方法定义: def push(elem: A):Stack.this.type
返回类型。它在堆栈顶部添加一个元素。
例子 #1:
// Scala program of push()
// method
import scala.collection.mutable.Stack
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a stack
var s = Stack[Int]()
// Applying push method
s.push(1)
s.push(2)
s.push(3)
s.push(4)
// Print the Stack
println(s)
}
}
输出。
Stack(4, 3, 2, 1)
例子#2。
// Scala program of push()
// method
import scala.collection.mutable.Stack
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a stack
var s = Stack[String]()
// Applying push method
s.push("C++")
s.push("Java")
s.push("Python")
s.push("Scala")
// Print the Stack
println(s)
}
}
输出。
Stack(Scala, Python, Java, C++)
极客教程