O'Reilly logo

Java Garage by Eben Hewitt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using Stacks

Stacks are like Pez dispensers. Stacks are very similar to arrays, and so we'll discuss them here. Many internal data structures are stored as stacks within Java. The stack operates on a last in, first out basis. You can create your own stack, or you can use an instance of java.util.Stack.

The java.util.Stack class extends java.util.Vector, which is a type of collection. The two main operations you use with a stack are pop() and push(). You can also peek() to see the item on the top of the stack. Finally, you can search the stack for an item, and you can determine if the stack is empty.

UsingStacks.java

 package net.javagarage.arrays; import java.util.Stack; /** * <p>Does stuff with java.util.Stack * @author eben hewitt */ public ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required