Java中栈的实现
来源:互联网 发布:软件开发方案书 编辑:程序博客网 时间:2024/06/06 10:42
Stack是java集合类中较为常见的一种数据类型,继承了Vector类,是一种先入后出的对象堆栈。它通过五个操作对类 <tt>Vector</tt> 进行了扩展 ,允许将向量视为堆栈。它提供了通常的push和pop操作,以及取堆栈顶点的peek方法、测试堆栈是否为空的empty方法、在堆栈中查找项并确定到堆栈顶距离的search方法。顺带研究了Jdk源码(JDk1.6)
empty:
* Tests if this stack is empty. * * @return <code>true</code> if and only if this stack contains * no items; <code>false</code> otherwise. */ public boolean empty() { return size() == 0; }size()方法继承于vector类,而vertor中有一个elementCount变量用于计算容器大小
* Returns the number of components in this vector. * * @return the number of components in this vector */ public synchronized int size() { return elementCount; }
peek:
/** * Looks at the object at the top of this stack without removing it * from the stack. * * @return the object at the top of this stack (the last item * of the <tt>Vector</tt> object). * @throws EmptyStackException if this stack is empty. */ public synchronized E peek() { int len = size(); if (len == 0) throw new EmptyStackException(); return elementAt(len - 1); }peek方法用于查看堆栈顶部对象,但不从堆栈中移除他,返回对象顶部元素(vector中的最后一项),若堆栈为空,就抛出EmptyStackException异常。element方法来自于vector类中,代码如下:
/** * Returns the component at the specified index. * * <p>This method is identical in functionality to the {@link #get(int)} * method (which is part of the {@link List} interface). * * @param index an index into this vector * @return the component at the specified index * @throws ArrayIndexOutOfBoundsException if the index is out of range * ({@code index < 0 || index >= size()}) */ public synchronized E elementAt(int index) { if (index >= elementCount) { throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); } return elementData(index); }在vector类中,有一个object类型的数字elementData数组来存储元素,所以要访问栈顶元素就是找到elementData中最后的那个索引值。
pop:
用于移除堆栈顶部的对象,并作为此函数的值返回该对象(即Vector 对象中的最后一项)。若堆栈为空,就抛出EmptyStackException异常。
/** * Removes the object at the top of this stack and returns that * object as the value of this function. * * @return The object at the top of this stack (the last item * of the <tt>Vector</tt> object). * @throws EmptyStackException if this stack is empty. */ public synchronized E pop() { E obj; int len = size(); obj = peek(); removeElementAt(len - 1); return obj; }可以看到,pop方法中首先是调用peek()方法,然后用removeElementAt方法移除栈顶元素。而remoElementAt方法也是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; if (j > 0) { System.arraycopy(elementData, index + 1, elementData, index, j); } elementCount--; elementData[elementCount] = null; /* to let gc do its work */ }
removeElementAt方法就是System.arraycopy方法,将elementData数组都向前移动一位,arraycopy中elementData即使原数组又是目标数组,index+1是原数组的起始下标,index是目标数组的起始下标,length是拷贝长度,因为vector中不一定是移除最后一个元素,所以才是从后面往前面开始复值覆盖。
push
把项压入堆栈顶部。
/** * Pushes an item onto the top of this stack. This has exactly * the same effect as: * <blockquote><pre> * addElement(item)</pre></blockquote> * * @param item the item to be pushed onto this stack. * @return the <code>item</code> argument. * @see java.util.Vector#addElement */ public E push(E item) { addElement(item); return item; }可以看到,它直接调用了vector中的addElement方法。
/** * Adds the specified component to the end of this vector, * increasing its size by one. The capacity of this vector is * increased if its size becomes greater than its capacity. * * <p>This method is identical in functionality to the * {@link #add(Object) add(E)} * method (which is part of the {@link List} interface). * * @param obj the component to be added */ public synchronized void addElement(E obj) { modCount++; ensureCapacityHelper(elementCount + 1); elementData[elementCount++] = obj; }addElement方法又有有几个封装好的函数组成的,说起来有点复杂,有兴趣的你可以直接去jdk源码中查看,这里就不细说了。
0 0
- Java中栈的实现
- java中栈与队列的实现
- Java中栈的实现原理
- Java中List的实现
- Java中Uuid的实现
- java中MDI的实现
- Java中DAO的实现
- Java中DAO的实现
- Java中List的实现
- java中系统托盘的实现
- Java中UUID的实现
- Java中DAO的实现
- Java中MDI的实现
- Java中DAO的实现
- java中多线程的实现
- JAVA中缓存的实现
- Java中DAO的实现
- java中ArrayList的实现
- JAVA之StringBuffer与StringBuilder
- 如何处理iOS中照片的方向
- 梯度下降法1
- 日经社説 20150605 日中韓FTAは高い水準の合意めざせ
- clang编译luabind项目报错和tcmalloc内存释放
- Java中栈的实现
- Android中基于Socket方式的文件上传
- 从今天开始记录一下每天学到的Android知识、以及开发中遇到的问题
- 社説 20150605 那覇離着陸ミス 管制官との交信解析がカギだ
- lucene 使用教程
- 在Mac OS X上安装Git
- 二维快速傅里叶变换及滤波
- 社説 20150605 日比首脳会談 平和と繁栄へ協調を続けたい
- 只要你想 并为之努力 你就能!