日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

java.util.Stack类简介

發(fā)布時(shí)間:2023/12/3 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java.util.Stack类简介 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

轉(zhuǎn)載自??java.util.Stack類簡(jiǎn)介

Stack是一個(gè)后進(jìn)先出(last in first out,LIFO)的堆棧,在Vector類的基礎(chǔ)上擴(kuò)展5個(gè)方法而來(lái)

Deque(雙端隊(duì)列)比起Stack具有更好的完整性和一致性,應(yīng)該被優(yōu)先使用

E push(E item)? ?
? ? ? ? ?把項(xiàng)壓入堆棧頂部。? ?
E pop()? ?
? ? ? ? ?移除堆棧頂部的對(duì)象,并作為此函數(shù)的值返回該對(duì)象。? ?
E peek()? ?
? ? ? ? ?查看堆棧頂部的對(duì)象,但不從堆棧中移除它。? ?
boolean empty()? ?
? ? ? ? ?測(cè)試堆棧是否為空。? ??
int search(Object o)? ?
? ? ? ? ?返回對(duì)象在堆棧中的位置,以 1 為基數(shù)。

Stack本身通過(guò)擴(kuò)展Vector而來(lái),而Vector本身是一個(gè)可增長(zhǎng)的對(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; }

通過(guò)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ù)不一致問(wèn)題屬于原子性問(wèn)題的范疇)

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? ? ? ?--和棧頂見(jiàn)的距離為2-1=1??
-1? ? ? --不存在于棧中??

?

?

Stack并不要求其中保存數(shù)據(jù)的唯一性,當(dāng)Stack中有多個(gè)相同的item時(shí),調(diào)用search方法,只返回與查找對(duì)象equal并且離棧頂最近的item與棧頂間距離(見(jiàn)源碼中search方法說(shuō)明)

?

?

總結(jié)

以上是生活随笔為你收集整理的java.util.Stack类简介的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。