java.util.Stack类简介
轉(zhuǎn)載自??java.util.Stack類簡介
Stack是一個(gè)后進(jìn)先出(last in first out,LIFO)的堆棧,在Vector類的基礎(chǔ)上擴(kuò)展5個(gè)方法而來
Deque(雙端隊(duì)列)比起Stack具有更好的完整性和一致性,應(yīng)該被優(yōu)先使用
E push(E item)? ?
? ? ? ? ?把項(xiàng)壓入堆棧頂部。? ?
E pop()? ?
? ? ? ? ?移除堆棧頂部的對(duì)象,并作為此函數(shù)的值返回該對(duì)象。? ?
E peek()? ?
? ? ? ? ?查看堆棧頂部的對(duì)象,但不從堆棧中移除它。? ?
boolean empty()? ?
? ? ? ? ?測試堆棧是否為空。? ??
int search(Object o)? ?
? ? ? ? ?返回對(duì)象在堆棧中的位置,以 1 為基數(shù)。
Stack本身通過擴(kuò)展Vector而來,而Vector本身是一個(gè)可增長的對(duì)象數(shù)組(?a growable array of?objects)那么這個(gè)數(shù)組的哪里作為Stack的棧頂,哪里作為Stack的棧底?
答案只能從源代碼中尋找,jdk1.6:
public class Stack<E> extends Vector<E> { /** * Creates an empty Stack. */ public Stack() { } /** * 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; } /** * 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). * @exception EmptyStackException if this stack is empty. */ public synchronized E pop() { E obj; int len = size(); obj = peek(); removeElementAt(len - 1); return obj; } /** * 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). * @exception 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; } /** * 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; } /** use serialVersionUID from JDK 1.0.2 for interoperability */ private static final long serialVersionUID = 1224463164541339165L; }通過peek()方法注釋The object at the top of this stack (the last item?of the Vector object,可以發(fā)現(xiàn)數(shù)組(Vector)的最后一位即為Stack的棧頂
pop、peek以及search方法本身進(jìn)行了同步
push方法調(diào)用了父類的addElement方法
empty方法調(diào)用了父類的size方法
Vector類為線程安全類
綜上,Stack類為線程安全類(多個(gè)方法調(diào)用而產(chǎn)生的數(shù)據(jù)不一致問題屬于原子性問題的范疇)
public class Test { public static void main(String[] args) { Stack<String> s = new Stack<String>(); System.out.println("------isEmpty"); System.out.println(s.isEmpty()); System.out.println("------push"); s.push("1"); s.push("2"); s.push("3"); Test.it(s); System.out.println("------pop"); String str = s.pop(); System.out.println(str); Test.it(s); System.out.println("------peek"); str = s.peek(); System.out.println(str); Test.it(s); System.out.println("------search"); int i = s.search("2"); System.out.println(i); i = s.search("1"); System.out.println(i); i = s.search("none"); System.out.println(i); } public static void it(Stack<String> s){ System.out.print("iterator:"); Iterator<String> it = s.iterator(); while(it.hasNext()){ System.out.print(it.next()+";"); } System.out.print("\n"); } }結(jié)果:
------isEmpty??
true? ? ? ? ? ??
------push??
iterator:1;2;3;? ??
------pop??
3? ? ? ?--棧頂是數(shù)組最后一個(gè)??
iterator:1;2;??
------peek??
2? ? ? ?--pop取后刪掉,peek只取不刪??
iterator:1;2;??
------search? ? ??
1? ? ? ?--以1為基數(shù),即棧頂為1??
2? ? ? ?--和棧頂見的距離為2-1=1??
-1? ? ? --不存在于棧中??
?
?
Stack并不要求其中保存數(shù)據(jù)的唯一性,當(dāng)Stack中有多個(gè)相同的item時(shí),調(diào)用search方法,只返回與查找對(duì)象equal并且離棧頂最近的item與棧頂間距離(見源碼中search方法說明)
?
?
總結(jié)
以上是生活随笔為你收集整理的java.util.Stack类简介的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 3000元的电脑配置(3000配置电脑)
- 下一篇: 3000块钱电脑配置推荐(电脑配置300