【java】【java Collection】Stack


源代码

package java.util;

/**
 * 先进后出  push pop peek empty search
 * The Stack class represents a last-in-first-out (LIFO) stack of objects. 
 * It extends class Vector with five operations that allow a vector to be treated as a stack. The usual
 * push and pop operations are provided, as well as a
 * method to peek at the top item on the stack, a method to test
 * for whether the stack is empty, and a method to search
 * the stack for an item and discover how far it is from the top.
 *
 * When a stack is first created, it contains no items.
 *
 * A more complete and consistent set of LIFO stack operations is
 * provided by the Deque(双端队列) interface and its implementations, which
 * should be used in preference to this class.  For example:
 *   Deque<Integer> stack = new ArrayDeque<Integer>();
 */

public
class Stack<E> extends Vector<E> {
    //因为是继承Vector  而Vector是通过数组实现的 也就意味着Stack也是通过数组实现的,而非链表
    
    //Creates an empty Stack
    public Stack() {
    }

   /*
    Vector中的源代码

    public synchronized void addElement(E obj) {
        modCount++;
        ensureCapacityHelper(elementCount + 1);//确保容量
        elementData[elementCount++] = obj;
    }

   */
    public E push(E item) {
        addElement(item);
        return item;
    }

   /*
    Vector中的源代码
    public synchronized int size() {
        return elementCount;
    }
   */
    public synchronized E pop() {
        E       obj;
        int     len = size();
        obj = peek();
        removeElementAt(len - 1);
        return obj;
    }

   
    public synchronized E peek() {
        int     len = size();

        if (len == 0)
            throw new EmptyStackException();
        return elementAt(len - 1);
    }

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


    /*
      public synchronized int lastIndexOf(Object o, int index) {
        if (index >= elementCount)
            throw new IndexOutOfBoundsException(index + " >= "+ elementCount);

        if (o == null) {
            for (int i = index; i >= 0; i--)
                if (elementData[i]==null)
                    return i;
        } else {
            for (int i = index; i >= 0; i--)
                if (o.equals(elementData[i]))
                    return i;
        }
        return -1;
    }


    public synchronized int lastIndexOf(Object o) {
        return lastIndexOf(o, elementCount-1);
    }
    */

    //相对最后元素开始计算  比如说1 2 3 4 5 search(2)的结果就是4
    public synchronized int search(Object o) {
        int i = lastIndexOf(o);

        if (i >= 0) {
            return size() - i;
        }
        return -1;
    }

    private static final long serialVersionUID = 1224463164541339165L;
}



示例程序

package Test.fast;

import java.util.Stack;
import java.util.Iterator;
import java.util.List;

public class StackTest {

    public static void main(String[] args) {
        Stack stack = new Stack();
        // 将1,2,3,4,5添加到栈中
        for(int i=1; i<6; i++) {
            stack.push(String.valueOf(i));
        }

        // 遍历并打印出该栈
        iteratorThroughRandomAccess(stack) ;

        // 查找“2”在栈中的位置,并输出
        int pos = stack.search("2");
        System.out.println("the postion of 2 is:"+pos);

        // pup栈顶元素之后,遍历栈
        stack.pop();
        iteratorThroughRandomAccess(stack) ;

        // peek栈顶元素之后,遍历栈
        String val = (String)stack.peek();
        System.out.println("peek:"+val);
        iteratorThroughRandomAccess(stack) ;

        // 通过Iterator去遍历Stack
        iteratorThroughIterator(stack) ;
    }

    /**
     * 通过快速访问遍历Stack
     */
    public static void iteratorThroughRandomAccess(List list) {
        String val = null;
        for (int i=0; i<list.size(); i++) {
            val = (String)list.get(i);
            System.out.print(val+" ");
        }
        System.out.println();
    }

    /**
     * 通过迭代器遍历Stack
     */
    public static void iteratorThroughIterator(List list) {

        String val = null;
        for(Iterator iter = list.iterator(); iter.hasNext(); ) {
            val = (String)iter.next();
            System.out.print(val+" ");
        }
        System.out.println();
    }

}


参考资料:

官方API

http://www.cnblogs.com/skywang12345/p/3308852.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值