You have basically to perform operations on the array using the FIFO propriety.
public ArrayStack( )
{
theArray = new String[InitialSize];
topOfStack = -1;
}
Every time you add an element to your array (push) you do topOfStack++;
and insert the element on the "topOfStack"
position of the array. By doing topOfStack++
you are keeping track of the array position that became the new top of the stack.
When you do pop
you must check if the array is not empty, return the element on array[topOfStack]
and do topOfStack--;
. Because the top is now the previous position on the array.
If you need more space for your array because the stack is full (topOfStack == InitialSize)
you have to create another array with more space, for example :
private void doubleArray( )
{
String [] newArray = new String[ theArray.length * 2 ];
for( int i = 0; i < theArray.length; i++ )
newArray[ i ] = theArray[ i ];
theArray = newArray;
}
For stating, this is roughly what you need to look for, naturally, there are many more details to look into. Nevertheless, try it out yourself, and when you have doubts look for resources such as this.