m865.datastructures
Class StackAL<T>

java.lang.Object
  extended by m865.datastructures.AbstractStack<T>
      extended by m865.datastructures.StackAL<T>
All Implemented Interfaces:
java.lang.Iterable<T>, java.util.Collection<T>

public class StackAL<T>
extends AbstractStack<T>

This class implements a Stack using a dynamic array Version 3.0 incorporates generics


Nested Class Summary
protected  class StackAL.StackALIterator<T>
          An iterator for an ArrayList stack.
 
Field Summary
protected  java.util.ArrayList<T> s
          The stack is implemented using a dynamic array from java.util.ArrayList
 
Fields inherited from class m865.datastructures.AbstractStack
hash
 
Constructor Summary
  StackAL()
          Constructs a stack whose dynamic array has the default initial size for a ArrayList.
  StackAL(java.util.Collection<? extends T> c)
          Constructs a stack which is initialized with the objects in the specified collection
  StackAL(int capacity)
          Constructs a stack whose dynamic array has a specified initial size.
protected StackAL(int hash, java.util.ArrayList<T> s)
          Constructs a stack with a specified hash code and a clone of the specified ArrayList.
 
Method Summary
 void clear()
          Removes all the objects from this stack.
 java.lang.Object clone()
          The Cloneable Interface indicates that the clone method, which is inherited from Object, is implemented so that a true clone (a true and independent copy) of the object is returned.
 java.util.Iterator<T> iterator()
          A factory method which returns an Iterator to the collection in this stack.
static void main(java.lang.String[] args)
          This main method tests the StackAL class to insure that the elementary functions are correct.
 T peek()
          Returns the object on the top of the stack.
 T pop()
          Removes and returns the object on the top of the stack.
 void push(T x)
          Pushes a type T object onto the top of the stack.
 int size()
          Determines the size of this stack.
 java.lang.String toString()
          List the objects in the stack
 
Methods inherited from class m865.datastructures.AbstractStack
add, addAll, contains, containsAll, downdateHashCode, equals, hashCode, isEmpty, remove, removeAll, retainAll, toArray, toArray, updateHashCode
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

s

protected java.util.ArrayList<T> s
The stack is implemented using a dynamic array from java.util.ArrayList

Constructor Detail

StackAL

public StackAL(int capacity)
Constructs a stack whose dynamic array has a specified initial size. Whenever the dynamic array is increased, it's size will determined by the default ArrayList strategy, which traditionally doubles the size.

Parameters:
capacity - the initial size of the dynamic array.

StackAL

public StackAL()
Constructs a stack whose dynamic array has the default initial size for a ArrayList.


StackAL

public StackAL(java.util.Collection<? extends T> c)
Constructs a stack which is initialized with the objects in the specified collection

Parameters:
c - the collection of objects to be initially pushed onto this stack.

StackAL

protected StackAL(int hash,
                  java.util.ArrayList<T> s)
Constructs a stack with a specified hash code and a clone of the specified ArrayList. This constuctor is only for use within the class and its subclasses.

Parameters:
hash - the hash code
s - the ArrayList to be cloned.
Method Detail

push

public void push(T x)
Pushes a type T object onto the top of the stack.

Specified by:
push in class AbstractStack<T>
Parameters:
x - the object to be placed on the top of the stack.

pop

public T pop()
Removes and returns the object on the top of the stack.

Specified by:
pop in class AbstractStack<T>
Returns:
the object on the top of the stack or null if the stack is empty.

peek

public T peek()
Returns the object on the top of the stack. The stack remains unchanged.

Specified by:
peek in class AbstractStack<T>
Returns:
the object on the top of the stack or null if the stack is empty.

clear

public void clear()
Removes all the objects from this stack. This is one of the optional operations specified by the Collection Interface


clone

public java.lang.Object clone()
The Cloneable Interface indicates that the clone method, which is inherited from Object, is implemented so that a true clone (a true and independent copy) of the object is returned.

Overrides:
clone in class java.lang.Object
Returns:
a clone (a true and independent copy) of this stack.

iterator

public java.util.Iterator<T> iterator()
A factory method which returns an Iterator to the collection in this stack. This iterator will walk throught the stack from the top to the bottom. It will throw a ConcurrentModificationException if the stack is altered during the iteration.

Returns:
a StackALIterator which will iterate through this stack starting from the top.

size

public int size()
Determines the size of this stack.

Returns:
the number of objects in this stack.

toString

public java.lang.String toString()
List the objects in the stack

Overrides:
toString in class AbstractStack<T>
Returns:
a formatted string listing the objects in this stack

main

public static void main(java.lang.String[] args)
This main method tests the StackAL class to insure that the elementary functions are correct.

Parameters:
args - optional command line arguments which will be ignored.