Scala Stack clear() 方法与示例
在 Scala Stack class
中, clear()方法用于删除堆栈的所有元素。
Method Definition: def clear(): Unit
Return Type: It returns an empty queue.
示例 #1:
// Scala program of clear()
// method
// Import Stack
import scala.collection.mutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating stack
val s1 = Stack(1, 2, 3, 4, 5)
// Print the stack
println("Before clear() method: " + s1)
// Applying clear method
val result = s1.clear
// Displays output
print("After clear() method: " + result)
}
}
输出:
Before clear() method: Stack(1, 2, 3, 4, 5)
After clear() method: ()
示例 #2:
// Scala program of clear()
// method
// Import Stack
import scala.collection.mutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating stack
val s1 = Stack("geeks", "for", "geeks")
// Print the stack
println("Before clear() method: " + s1)
// Applying clear method
val result = s1.clear
// Displays output
print("After clear() method: " + result)
}
}
输出:
Before clear() method: Stack(geeks, for, geeks)
After clear() method: ()