Datasets:
ArXiv:
License:
File size: 763 Bytes
c574d3a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package rd222dv_assign1.intCollection;
import java.util.NoSuchElementException;
public class ArrayIntStack extends AbstractIntCollection implements IntStack {
@Override
public void push(int n) {
if (size == values.length) {
throw new IllegalStateException("Cannot add to full stack");
}
values[size++] = n;
}
@Override
public int pop() throws IndexOutOfBoundsException {
if (size == 0) {
throw new NoSuchElementException("Cannot pop from empty stack");
}
int value = values[size-1];
size--;
return value;
}
@Override
public int peek() throws IndexOutOfBoundsException {
if (size == 0) {
throw new NoSuchElementException("Cannot peek into empty stack");
}
return values[size - 1];
}
}
|