I'm trying to implement a drop out stack in Java and it's currently giving me a run for my money! Haha
I've gotten this far, and as far as I can tell, my logic is sound, but it's not compiling. I keep getting a java.lang.ArrayIndexOutOfBoundsException...
So here's the down and dirty of what I'm trying to do: I'm pushing and popping a series of elements in a stack, and would like the bottom element to drop out when a new element is added to the top of the stack. Any suggestions?
My Code:
import java.util.Arrays;
public class Base_A05Q2
{
/**
* Program entry point for drop-out stack testing.
* @param args Argument list.
*/
public static void main(String[] args)
{
ArrayDropOutStack<Integer> stack = new ArrayDropOutStack<Integer>(4);
System.out.println("DROP-OUT STACK TESTING");
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
System.out.println("The size of the stack is: " + stack.size());
if(!stack.isEmpty())
System.out.println("The stack contains:\n" + stack.toString());
stack.pop();
stack.push(7);
stack.push(8);
System.out.println("The size of the stack is: " + stack.size());
if(!stack.isEmpty())
System.out.println("The stack contains:\n" + stack.toString());
}
public static class ArrayDropOutStack<T> implements StackADT<T>
{
private final static int DEFAULT_CAPACITY = 100;
private int top;
private int bottomElem = 0;
private T[] stack;
/**
* Creates an empty stack using the default capacity.
*/
public ArrayDropOutStack()
{
this(DEFAULT_CAPACITY);
}
/**
* Creates an empty stack using the specified capacity.
* @param initialCapacity the initial size of the array
*/
@SuppressWarnings("unchecked")
public ArrayDropOutStack(int initialCapacity)
{
top = -1;
stack = (T[])(new Object[initialCapacity]);
}
/**
* Adds the specified element to the top of this stack, expanding
* the capacity of the array if necessary.
* @param element generic element to be pushed onto stack
*/
public void push(T element)
{
if (size() == stack.length)
top = 0;
stack[top] = element;
top++;
}
/**
* Removes the element at the top of this stack and returns a
* reference to it.
* @return element removed from top of stack
* @throws EmptyCollectionException if stack is empty
*/
public T pop() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException("stack");
T result = stack[top];
stack[top] = null;
if (top == 0)
top = size()-1;
top--;
return result;
}
/**
* Returns a reference to the element at the top of this stack.
* The element is not removed from the stack.
* @return element on top of stack
* @throws EmptyCollectionException if stack is empty
*/
public T peek() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException("stack");
return stack[top];
}
/**
* Returns true if this stack is empty and false otherwise.
* @return true if this stack is empty
*/
public boolean isEmpty()
{
if(stack.length == 0)
{
return true;
}
else
{
return false;
}
}
/**
* Returns the number of elements in this stack.
* @return the number of elements in the stack
*/
public int size()
{
int counter = 0;
for (int i = 0; i < stack.length; i++)
{
if (stack[i] != null)
{
//counter ++;
}
}
return counter;
}
/**
* Returns a string representation of this stack. The string has the
* form of each element printed on its own line, with the top most
* element displayed first, and the bottom most element displayed last.
* If the list is empty, returns the word "empty".
* @return a string representation of the stack
*/
public String toString()
{
String result = "";
for (int scan = top-1; scan >= 0; scan--)
result = result + stack[scan].toString() + "\n";
return result;
}
}
}
I think the problem is in this block, but I can't pin down the problem. Any help is greatly appreciated!
public void push(T element)
{
if (size() == stack.length)
top = 0;
stack[top] = element;
top++;
}