Java之--Stack栈

来源:互联网 发布:mac safari缓存文件 编辑:程序博客网 时间:2024/06/14 07:17

Stack栈
====================================================
后进先出】的结构

Stack本身通过扩展Vector而来,而Vector本身是一个可增长的对象数组(a growable array of objects)

栈的数组结构实现


jdk1.7(其它版本可能不同)
---------------------------------------------------------
Stack.class   5个方法
E push(E item)             把项压入栈顶部。            
E pop()                           移除栈顶部的对象,并作为此函数的值返回该对象。          
E peek()                         查看栈顶部的对象,但不从堆栈中移除它。         
boolean empty()           测试栈是否为空。            
int search(Object o)     返回对象在栈中的位置,以1为基数。      
---------------------------------------------------------
public class Stack<E> extends Vector<E>{}

public class Vector<E> extends AbstractList<E> implements List<E>, ...{
    protected Object[] elementData;     //数组
    protected int elementCount;             //数组中元素个数
    protected int capacityIncrement;      //自动递增量(大小)
}

0.初始化
Stack stack = new Stack();//就这一个构造方法
会调用Vector的构造方法,Vector线程安全
public Vector() {
    this(10);
}
public Vector(int initialCapacity, int capacityIncrement) {
    this.elementData = new Object[initialCapacity];     //初始容量为10
    this.capacityIncrement = capacityIncrement;          //初始递增量为0
}



1.push()把对象压入栈顶
public E push(E item) {
       addElement(item);
        return item;
}
public synchronized void addElement(E obj) {
        modCount++;
        ensureCapacityHelper(elementCount + 1);//确保需不需要扩容
        elementData[elementCount++] = obj;//elementCount++数组中元素个数加1;加入的数据依次往后放
}
private void ensureCapacityHelper(int minCapacity) {
        if (minCapacity - elementData.length > 0)//大于数组中元素个数时扩容
            grow(minCapacity);//扩容
}

//Stack stcapacityIncrement=0,扩容量为原来的一倍
//原来elementData.length=10,扩容后为20,再扩容后为40
private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
                                         capacityIncrement : oldCapacity);//如果capacityIncrement=0,扩容量为原来的一倍
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        if (newCapacity - MAX_ARRAY_SIZE > 0)//MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
            newCapacity = hugeCapacity(minCapacity);
        elementData = Arrays.copyOf(elementData, newCapacity);
}

2.pop()移除栈顶部的对象
public synchronized E pop() {
        E obj = peek();//栈顶对象
        removeElementAt(size() - 1);//移除栈顶对象

        return obj;//返回栈顶对象
    }
public synchronized void removeElementAt(int index) {
      ......  
        elementCount--;
        elementData[elementCount] = null;
    }

3.peek()查看栈顶部的对象
//return the object at the top of this stack (the last item of the Vector object).
public synchronized E peek() {
        int     len = size();

        if (len == 0)
            throw new EmptyStackException();
        return elementAt(len - 1);
    }
相当于 elementData[size() - 1]

4.empty()
public boolean empty() {
        return size() == 0;
    }

5. search()返回对象在栈中的位置
public synchronized int search(Object o) {
        int i = lastIndexOf(o);

        if (i >= 0) {
            return size() - i;//对象在栈中的位置,以1为基数;从栈顶到栈底的顺序
        }
        return -1;
    }
public synchronized int lastIndexOf(Object o, int elementCount-1) {
        ......
        for (int i = index; i >= 0; i--)//从数组后面往前找(相当于从栈顶往栈底找)
            if (o.equals(elementData[i]))
               return i;
        
        return -1;
    }

Stack不要求其中保存数据的唯一性,当Stack中有多个相同的item时,调用search方法,

只返回与查找对象equal并且离栈顶最近的item与栈顶间距离


0 0
原创粉丝点击