Array StacksConcrete Mutable Collection ClassesArray SequencesStacksContents

Stacks

You saw immutable stacks earlier. There is also a mutable version, supported by class mutable.Stack. It works exactly the same as the immutable version except that modifications happen in place.

scala> val stack = new scala.collection.mutable.Stack[Int]           
stack: scala.collection.mutable.Stack[Int] = Stack()
scala> stack.push(1)
res0: stack.type = Stack(1)
scala> stack
res1: scala.collection.mutable.Stack[Int] = Stack(1)
scala> stack.push(2)
res0: stack.type = Stack(1, 2)
scala> stack
res3: scala.collection.mutable.Stack[Int] = Stack(1, 2)
scala> stack.top
res8: Int = 2
scala> stack
res9: scala.collection.mutable.Stack[Int] = Stack(1, 2)
scala> stack.pop    
res10: Int = 2
scala> stack    
res11: scala.collection.mutable.Stack[Int] = Stack(1)

Next: Array Stacks


Array StacksConcrete Mutable Collection ClassesArray SequencesStacksContents