Stack
java.util.Stack
caution
Use ArrayDeque
for using Stack
:~
The Standard Class Library provides the Stack
class, but, according to JavaDoc
, a more complete and consistent set of LIFO
stack operations is provided by the Deque
Interface and its implementations, which should be used in preference to this class. So, it is recommended to use Deque for stacks
.
caution
Must Use
ArrayDeque
class to declareStack
as it contains all the methods of Stack and can also implementQueue
as well.
Stack
cannot usefor_Each
loop to traverse through its elements.Iterator
class with its methods.hasNext()
&.next()
must be used to print and traverse through all its elements.Stack
usesLast In First Out (LIFO)
Stack
can only usepush()
to add elements andpop()
to remove and print elements and