Stack class

 

see Collection interface and additionally List interface for descriptions and examples of Stack's inherited methods.

 

Stack is part of List. It extends Vector, adding five methods which provide stacking operations. You can put things on - or take things off - the top of the stack with .push(...) and .pop( ). You can peek at the top of the stack with .peek( ). You can test for whether the stack is empty with .empty( ). And you can search for an item and learn its position in the stack with .search(...) .

 

Stack, like Vector, is an older Java class, from before Java v1.2.

 

■ You must cast its returns, as with Vector.

 

■ When a Stack is first created, it contains no items. That's because it has just one constructor.

 

Stack st = new Stack( ): creates an empty Stack

 

■ Below are the five methods which Stack adds to Vector. These can only be used with Stack. They cannot be used with Vector nor with any other Lists.

 

 

boolean .empty( ) method

 

■ This method returns a boolean true if the Stack has no elements in it. It is the same as the

isEmpty( ) method usable with all Collections. i.e. The snippet doubly demonstrates how a newly created Stack is indeed empty.

 

import java.util.*;

 

Stack st = new Stack( );

if ( st.empty( ) ) System.out.println("STACK IS EMPTY");

if ( st.isEmpty( ) ) System.out.println("STACK IS EMPTY");

 

■ This snippet loops through a stack's elements, popping them out in turn. It prints:

 

Top contains: 3

Popped: 3

Popped: 2

Popped: 1

 

import java.util.*;

 

Stack st = new Stack( );

st.push( "1" );

st.push( "2" );

st.push( "3" );

String top =(String) st.peek( );

System.out.println("Top contains: " + top);

do {

top = (String) st.pop( );

System.out.println("Popped: " + top);

} while (!st.empty( ));

 

 

Object .peek( ) method

 

■ This method returns the last (top) object from the Stack, without modifying anything. It is the same as the lastElement( ) method provided by Vector. It is also the same as saying .get( size( ) -1 ). The snippet shows four ways to get at that same top element. i.e.

 

import java.util.*;

 

Stack st = new Stack( );

st.add( "FIRST ELEMENT" );

st.add( "MIDDLE ELEMENT" );

st.add( "LAST ELEMENT" );

System.out.println( st.peek( ) );

System.out.println( st.lastElement( ) );

System.out.println( st.get( st.size( )-1) );

System.out.println( st.elementAt( st.size( )-1) );

 

■ Note that there is no other peek method with an index parameter. So you cannot peek at any other item in the stack. You have to use .get( index ).

 

Object .pop( ) method

 

■ This method returns and also removes the last (top) element from the Stack. It acts the removeElementAt(...) method provided by Vector and is the same as saying .remove( size( ) -1 ). i.e. The snippet shows three ways to remove that same top element. It prints nothing because the three statements remove the Stack's three elements in turn.

 

import java.util.*;

 

Stack st = new Stack( );

st.add( "FIRST ELEMENT" );

st.add( "MIDDLE ELEMENT" );

st.add( "LAST ELEMENT" );

st.pop( ); // remove first

st.remove( st.size( )-1); // remove first

st.removeElementAt( st.size( )-1); // remove first

System.out.println( st );

 

 

Object .push( Obj ) method

 

■ This method places the supplied object onto the top of the list. It is the same as Vector's addElement(...) method and the same as saying .add(...). The snippet demonstratess five ways to push items onto the top of a list:

 

import java.util.*;

 

Stack st = new Stack( );

st.push( "1" );

st.add( "2" );

st.addElement( "3" );

st.add( st.size( ), "4");

st.insertElementAt( "5", st.size( ) );

System.out.println( st );

 

 

int .search( Obj ) method

 

■ This method uses the .equals(...) method to search for the first occurrence of a specified object element. If one is found, it returns its distance from top. The top is considered 1, not 0. The method returns 1 if the object is not found. The snippet prints:

 

The C is at index: 1

The A is at index: 3

D is not in the stack

 

import java.util.*;

 

Stack st = new Stack( );

st.push( "A" );

st.push( "B" );

st.push( "C" );

System.out.println( "The C is at index: " + st.search( "C" ) );

System.out.println( "The A is at index: " + st.search( "A" ) );

int location = st.search( "D" );

if ( location > 0) {

System.out.println("D is at index " + location );

}

else {

System.out.println("D is not in the stack");

}