Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ArrayList<Object> array;
- public ArrayListStack() {
- array = new ArrayList<Object>();
- }
- public void push(Object o) {
- array.add(o);
- }
- public Object pop() {
- if(array.size() == 0) {
- throw new NoSuchElementException("The stack is empty");
- } else {
- Object toReturn = array.get(array.size()-1);
- array.remove(array.size()-1);
- return toReturn;
- }
- }
- public Object peek() {
- if(array.size() == 0) {
- throw new NoSuchElementException("The stack is empty");
- } else {
- return array.get(array.size()-1);
- }
- }
- public boolean isEmpty() {
- return array.size() == 0;
- }
- public int size() {
- return array.size();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement