List之Stack源码分析
来源:互联网 发布:dijkstra算法的应用 编辑:程序博客网 时间:2024/05/17 13:07
源码版本为JDK1.7.0_75。
该类继承自Vector,说明该类是可克隆的、可序列化的,且是同步的。
publicclass Stack<E> extends Vector<E>
构造函数
public Stack() { }
入栈
/** * 将一个元素放入栈顶,通过vector类的addElement方法实现 * 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; }
出栈
/** * 删除栈顶元素并将其返回,使用peek方法获取栈顶元素,使用vector类的removeElementAt方法完成元素的删除 * 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; } /** * 查看栈顶元素,但不将其从栈顶删除,使用vector类的elementAt方法实现。 * 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); }
是否为空
/** * 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; }
搜索
/** * vector从n-1到0对应栈顶到栈底,因此在搜索时,应使用lastIndexOf方法 * Returns the 1-based position where an object is on this stack. * If the object <tt>o</tt> occurs as an item in this stack, this * method returns the distance from the top of the stack of the * occurrence nearest the top of the stack; the topmost item on the * stack is considered to be at distance <tt>1</tt>. The <tt>equals</tt> * method is used to compare <tt>o</tt> to the * items in this stack. * * @param o the desired object. * @return the 1-based position from the top of the stack where * the object is located; the return value <code>-1</code> * indicates that the object is not on the stack. */ public synchronized int search(Object o) { int i = lastIndexOf(o); if (i >= 0) { return size() - i; } return -1; }
0 0
- List之Stack源码分析
- Java集合之Stack 源码分析
- List源码解析之ArrayList源码分析
- List源码解析之Vector 源码分析
- List源码解析之LinkedList 源码分析
- List源码解析之LinkedList 源码分析
- Stack源码分析
- Stack源码分析
- 《Java源码分析》:Stack
- Stack源码分析
- Stack源码分析
- 《Java源码分析》:Stack
- Java集合之Stack 源码分析 泽…
- 数据结构之栈(附:Stack/Vector源码分析)
- 基础数据机构之Stack栈源码分析
- Android Gems - Java源码分析之List
- C++ STL源码分析之 list
- Java源码之Stack
- Zoho Books:经常性交易自动扣款
- 115个Java面试题和答案——(上)
- ApacheBench(ab)测试工具小结
- JSONHelper
- bug(1)
- List之Stack源码分析
- MongoDB 进阶(命令\固定集合)
- 梦行Monxin企业建站系统(PHP源码,带PC站,手机站,微官网,APP建站系统,傻瓜式自助建站系统) 橙色版
- 115个Java面试题和答案——(下)
- 学C++就学服务端,先把apue和unp两卷看了,接着libevent,出来找工作应该没问题
- poj1743Musical Theme【后缀数组求最长不重叠重复子串】楼教主男人八题
- iOS 怎样创建不带storyboard的工程(或只带xib的工程)
- POM.xml详解
- Excel2013无法打开HTTPS网站的xls文件