jdk中栈的实现

来源:互联网 发布:linux添加用户密码 编辑:程序博客网 时间:2024/06/06 03:01

栈只允许访问一个数据项:即最后插入的数据项。
只能对栈顶进行操作,后进先出(LIFO)。

push操作:

public E push(E item) {        addElement(item);        return item;}其中addElement(item)是父类vector的一个方法:public synchronized void addElement(E obj) {        modCount++;        ensureCapacityHelper(elementCount + 1);        elementData[elementCount++] = obj;}其中modCount是定义在vector的父类AbstracList中的一个整数,在栈中,这个变量用来记录共有多少次进出栈操作。protected transient int modCount = 0;ensureCapacityHelper(int s)方法用来给数组容器扩容。protected int elementCount;是vector中的成员变量,用来记录当前共有多少可用值

pop操作,从栈中取出栈顶元素

  public synchronized E pop() {        E       obj;        int     len = size();        obj = peek();//获取当前栈顶元素        removeElementAt(len - 1);//移除栈顶元素        return obj;    }size()方法实现为public synchronized int size() {        return elementCount;}peek()方法是获取当前最后一个元素public synchronized E peek() {        int     len = size();        if (len == 0)            throw new EmptyStackException();        return elementAt(len - 1);}elementAt(int i)获取在位置i处的元素,该方法在vector中实现public synchronized E elementAt(int index) {        if (index >= elementCount) {            throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);        }        return elementData(index);    }E elementData(int index) {        return (E) elementData[index];}removeElementAt(int index)用来移除数组中位置Index处的元素,在vector中实现。public synchronized void removeElementAt(int index) {        modCount++;        if (index >= elementCount) {            throw new ArrayIndexOutOfBoundsException(index + " >= " +                                                     elementCount);        }        else if (index < 0) {            throw new ArrayIndexOutOfBoundsException(index);        }        int j = elementCount - index - 1;//在栈操作中,j是永远等于0的。        if (j > 0) {            System.arraycopy(elementData, index + 1, elementData, index, j);//把index处的元素移除,并把后面的元素前移一位。用C++实现。对于栈对象,这里不会执行        }        elementCount--;        elementData[elementCount] = null; /* 置为null,让gc及时回收*/    }
原创粉丝点击