I'm trying to create a Stack in Java that uses arrays for its implementation. Below is the pop() method in my self-defined stack class
public E pop()
{
if(data.length == 0)
{
throw new EmptyStackException();
}
E poppedObject = data[0];
for(int i = 0; i < data.length-1; i++) //Moving all the elements one closer to top
{
data[i] = data[i+1];
}
return poppedObject;
}
When all the data has been popped out of the stack and you try to pop something out of it, an EmptyStackException should be thrown. However, data.length does not change as objects are popped out. How is the pop method supposed to tell if a stack is empty or not if it can't tell with data.length?