SHARE
TWEET

Untitled

a guest Apr 25th, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.    
  2.     ArrayList<Object> array;
  3.    
  4.     public ArrayListStack() {
  5.         array = new ArrayList<Object>();
  6.     }
  7.    
  8.     public void push(Object o) {
  9.         array.add(o);
  10.     }
  11.    
  12.     public Object pop() {
  13.         if(array.size() == 0) {
  14.             throw new NoSuchElementException("The stack is empty");
  15.         } else {
  16.             Object toReturn = array.get(array.size()-1);
  17.             array.remove(array.size()-1);
  18.             return toReturn;
  19.         }
  20.     }
  21.    
  22.     public Object peek() {
  23.         if(array.size() == 0) {
  24.             throw new NoSuchElementException("The stack is empty");
  25.         } else {
  26.             return array.get(array.size()-1);
  27.         }
  28.     }
  29.    
  30.     public boolean isEmpty() {
  31.         return array.size() == 0;
  32.     }
  33.    
  34.     public int size() {
  35.         return array.size();
  36.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top