/** * Implementation of a stack by means of an array. * * @author Franck van Breugel */ public class ArrayStack implements Stack { private String[] content; private int size; //@ invariant this.content != null /** * Initializes this stack to be empty. */ public ArrayStack() { this.size = 0; this.setContent(new String[1]); } /** * Returns the content of this stack. * * @return the content of this stack. */ private String[] getContent() { return this.content; } /** * Sets the content of this stack to the given content. * * @param content the content of this stack. * @pre. pre. content ! null */ private void setContent(String[] content) { this.content = content; } /** * Returns the size of this stack. * * @return the size of this stack. */ private int getSize() { return this.size; } /** * Sets the size of this stack to the given size. * * @param size the size of this stack. */ private void setSize(int size) { this.size = size; } public boolean isEmpty() { return this.getSize() == 0; } public String pop() { this.setSize(this.getSize() - 1); String top = this.getContent()[this.getSize()]; this.getContent()[this.getSize()] = null; return top; } public void push(String element) { if (this.getContent().length == this.getSize()) { String[] temp = new String[this.getContent().length * 2]; for (int i = 0; i < this.getContent().length; i++) { temp[i] = this.getContent()[i]; } this.setContent(temp); } this.getContent()[this.getSize()] = element; this.setSize(this.getSize() + 1); } }