1 4 package com.tc.util; 5 6 import java.util.ArrayList ; 7 import java.util.EmptyStackException ; 8 import java.util.Iterator ; 9 import java.util.List ; 10 11 16 public class Stack { 17 18 private final List list = new ArrayList (); 19 20 23 public Stack() { 24 } 25 26 33 public Object push(Object item) { 34 list.add(item); 35 return item; 36 } 37 38 44 public Object pop() { 45 int len = size(); 46 47 if (len == 0) throw new EmptyStackException (); 48 return list.remove(len - 1); 49 } 50 51 57 public Object peek() { 58 int len = size(); 59 60 if (len == 0) throw new EmptyStackException (); 61 return list.get(len - 1); 62 } 63 64 69 public boolean empty() { 70 return size() == 0; 71 } 72 73 78 public int size() { 79 return list.size(); 80 } 81 82 92 public int search(Object o) { 93 int i = list.lastIndexOf(o); 94 95 if (i >= 0) { return size() - i; } 96 return -1; 97 } 98 99 private static final long serialVersionUID = 343422342343423234L; 100 101 102 103 104 public Object get(int index) { 105 return list.get(index); 106 } 107 108 public Object remove(int index) { 109 return list.remove(index); 110 } 111 112 public Iterator iterator() { 113 return list.iterator(); 114 } 115 116 public boolean isEmpty() { 117 return empty(); 118 } 119 120 public boolean contains(Object o) { 121 return list.contains(o); 122 } 123 124 public boolean remove(Object o) { 125 return list.remove(o); 126 } 127 } 128 | Popular Tags |