1 16 package java.util; 17 18 21 public class Stack extends Vector { 22 23 public Object clone() { 24 Stack s = new Stack (); 25 s.addAll(this); 26 return s; 27 } 28 29 public boolean empty() { 30 return isEmpty(); 31 } 32 33 public Object peek() { 34 int sz = size(); 35 if (sz > 0) { 36 return get(sz - 1); 37 } else { 38 throw new EmptyStackException (); 39 } 40 } 41 42 public Object pop() { 43 int sz = size(); 44 if (sz > 0) { 45 return remove(sz - 1); 46 } else { 47 throw new EmptyStackException (); 48 } 49 } 50 51 public Object push(Object o) { 52 add(o); 53 return o; 54 } 55 56 public int search(Object o) { 57 for (int i = 0, n = size(); i < n; ++i) { 58 Object other = get(i); 59 if (o == null ? other == null : o.equals(other)) { 60 return n - i; 61 } 62 } 63 return -1; 64 } 65 66 } 67 | Popular Tags |