Skip to main content

Stack

Learn Stack (List Interface, Collection Framework) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.

What is a Stack? A stack is a pile where you only touch the top . The last item you put on is the first item you take off: LIFO (Last In, First Out). Think of a pile of plates, or the Back button of a browser: the page you visited most recently is the first one you go back to. The class java.util.Stack extends Vector , so it is a growable array in which the end of the array is the top of the stack. Only five methods are special: push , pop , peek , empty and search . Because it extends Vector, it is synchronized (thread-safe, slower) and also exposes all Vector/List methods ( get(i) , add(i,...

More lessons in List Interface