Datasets:
ArXiv:
License:
| package rd222dv_assign1.intCollection; | |
| import java.util.NoSuchElementException; | |
| public class ArrayIntStack extends AbstractIntCollection implements IntStack { | |
| public void push(int n) { | |
| if (size == values.length) { | |
| throw new IllegalStateException("Cannot add to full stack"); | |
| } | |
| values[size++] = n; | |
| } | |
| public int pop() throws IndexOutOfBoundsException { | |
| if (size == 0) { | |
| throw new NoSuchElementException("Cannot pop from empty stack"); | |
| } | |
| int value = values[size-1]; | |
| size--; | |
| return value; | |
| } | |
| public int peek() throws IndexOutOfBoundsException { | |
| if (size == 0) { | |
| throw new NoSuchElementException("Cannot peek into empty stack"); | |
| } | |
| return values[size - 1]; | |
| } | |
| } | |