java中stack(棧)的使用代碼實(shí)例
java中stack類(lèi)繼承于vector,其特性為后進(jìn)先出(lastinfirstout).
入棧和出棧實(shí)例圖:
實(shí)例圖的java代碼實(shí)例:
package com.lanhuigu.java.ListTest; import java.util.Stack; public class StackTest { public static void main(String[] args) { Stack<String> staffs = new Stack<String>(); // 入棧順序: a,b,c,d,e staffs.push("a"); staffs.push("b"); staffs.push("c"); staffs.push("d"); staffs.push("e"); // 出棧順序: e,d,c,b,a while( !staffs.isEmpty()) { System.out.print(staffs.pop() + " "); } } }
程序運(yùn)行結(jié)果:
edcba
Stack類(lèi)中方法:
官網(wǎng)API:
方法分析:
empty():判斷棧是否為空,為空返回true,否則返回false
peek():取出棧頂元素,但是不從棧中移除元素
pop():取出棧頂元素,并且將其從棧中移除
push(Eitem):元素入棧
search(Objecto):在棧中查找元素位置,位置從棧頂開(kāi)始往下算,棧頂為1,
依次往下數(shù)到所查找元素位置,如果所查找元素在棧中不存在,則返回-1。
關(guān)于這幾個(gè)方法的實(shí)例:
package com.lanhuigu.java.ListTest; import java.util.Stack; public class StackMethodTest { public static void main(String[] args) { Stack<String> staffs = new Stack<String>(); // 入棧順序: a,b,c,d,e staffs.push("a"); staffs.push("b"); staffs.push("c"); staffs.push("d"); staffs.push("e"); System.out.println("empty():" + staffs.empty()); System.out.println("peek():" + staffs.peek()); System.out.println("search(Object o):" + staffs.search("a")); System.out.println("search(Object o):" + staffs.search("e")); System.out.println("search(Object o):" + staffs.search("no")); // 出棧順序: e,d,c,b,a while( !staffs.isEmpty()) { System.out.print(staffs.pop() + " "); } System.out.println("=====空棧中使用方法======="); System.out.println("empty():" + staffs.empty()); //System.out.println("peek():" + staffs.peek());// 在空棧中使用時(shí)報(bào)錯(cuò),因?yàn)闆](méi)有棧頂元素 System.out.println("search(Object o):" + staffs.search("a")); System.out.println("search(Object o):" + staffs.search("e")); System.out.println("search(Object o):" + staffs.search("no")); //System.out.print(staffs.pop());// 空棧中移除棧頂元素,報(bào)錯(cuò) } }
程序運(yùn)行結(jié)果:
以上幾個(gè)方法是Stack繼承于Vector擴(kuò)展的方法,因?yàn)镾tack繼承于Vector,哪么Vector中的非private方法
也是Stack類(lèi)的方法。
Vector中的方法,官方API_1.8:
總結(jié)
以上就是本文關(guān)于java中stack(棧)的使用代碼實(shí)例的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專(zhuān)題。如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
- JAVA中StackOverflowError錯(cuò)誤的解決
- JVM---jstack分析Java線程CPU占用,線程死鎖的解決
- Java中使用StackWalker和Stream API進(jìn)行堆棧遍歷
- Java StackTraceElement實(shí)例代碼
- Java線程Dump分析工具jstack解析及使用場(chǎng)景
- 深入分析JAVA Vector和Stack的具體用法
- Java反射之Call stack introspection詳解
- Java數(shù)據(jù)結(jié)構(gòu)與算法之棧(Stack)實(shí)現(xiàn)詳解
- OneinStack一鍵安裝PHP/JAVA/HHVM和超詳細(xì)的VPS手動(dòng)安裝LNMP的方法
- Java 異常的棧軌跡(Stack Trace)詳解及實(shí)例代碼
- java自帶的工具Jstack截取進(jìn)程中的堆棧信息
- Java 并發(fā)編程ArrayBlockingQueue的實(shí)現(xiàn)
- 詳解JAVA中priorityqueue的具體使用
- 詳解Java中的延時(shí)隊(duì)列 DelayQueue
- 詳解java中DelayQueue的使用
- java隊(duì)列之queue用法實(shí)例分析
- Java多線程工具篇BlockingQueue的詳解
- Java Stack與Queue詳解
相關(guān)文章
解決Springboot-application.properties中文亂碼問(wèn)題
這篇文章主要介紹了解決Springboot-application.properties中文亂碼問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11深入理解SpringBoot?最大連接數(shù)及最大并發(fā)數(shù)
SpringBoot能支持的最大并發(fā)量主要看其對(duì)Tomcat的設(shè)置,可以在配置文件中對(duì)其進(jìn)行更改,本文就來(lái)介紹一下SpringBoot?最大連接數(shù)及最大并發(fā)數(shù),感興趣的可以了解一下2023-08-08Java實(shí)現(xiàn)計(jì)算圖中兩個(gè)頂點(diǎn)的所有路徑
這篇文章主要為大家詳細(xì)介紹了如何利用Java語(yǔ)言實(shí)現(xiàn)計(jì)算圖中兩個(gè)頂點(diǎn)的所有路徑功能,文中通過(guò)示例詳細(xì)講解了實(shí)現(xiàn)的方法,需要的可以參考一下2022-10-10Spring Boot下如何自定義Repository中的DAO方法
這篇文章主要介紹了Spring Boot下如何自定義Repository中的DAO方法,需要的朋友可以參考下2017-06-06關(guān)于RequestMapping注解的作用說(shuō)明
這篇文章主要介紹了關(guān)于RequestMapping注解的作用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。2022-01-01Java并發(fā)編程數(shù)據(jù)庫(kù)與緩存數(shù)據(jù)一致性方案解析
這篇文章主要為大家介紹了Java并發(fā)編程中數(shù)據(jù)庫(kù)與緩存數(shù)據(jù)一致性解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04關(guān)于spring boot中幾種注入方法的一些個(gè)人看法
這篇文章主要給大家介紹了關(guān)于spring boot中幾種注入方法的一些個(gè)人看法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用spring boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07