实现

Stack 是基于 Vector 实现的,线程安全

push

添加元素到末尾

  1. public E push(E item) {
  2. addElement(item);
  3. return item;
  4. }

pop、peek

直接从末尾移除元素

  1. public synchronized E pop() {
  2. E obj;
  3. int len = size();
  4. obj = peek();
  5. removeElementAt(len - 1);
  6. return obj;
  7. }
  8. public synchronized E peek() {
  9. int len = size();
  10. if (len == 0)
  11. throw new EmptyStackException();
  12. return elementAt(len - 1);
  13. }