Stack

Stack继承于Vector,在其基础上实现了Stack要求的后进先出LIFO的弹出及压入操作,提供了push/pop/peek等主要方法

  • push():通过调用Vector中的addElement来完成
public E push(E item) {
    addElement(item);
    return item;
}
  • pop():通过调用peek()获取最后一个元素,并删除最后一个元素
public synchronized E pop() {
    E       obj;
    int     len = size();

    obj = peek();
    removeElementAt(len - 1);

    return obj;
}
  • peek():获取最后一个元素
public synchronized E peek() {
    int     len = size();
    if (len == 0)
        throw new EmptyStackException();
    return elementAt(len - 1);
}

results matching ""

    No results matching ""