Scala Stack clear() method with example
In Scala Stack class, the clear() method is utilized to delete all the elements of the stack.
Method Definition: def clear(): Unit
Return Type: It returns an empty queue.
Example #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) } } |
chevron_right
filter_none
Output:
Before clear() method: Stack(1, 2, 3, 4, 5) After clear() method: ()
Example #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) } } |
chevron_right
filter_none
Output:
Before clear() method: Stack(geeks, for, geeks) After clear() method: ()

