Decode360's Blog

          業(yè)精于勤而荒于嬉 QQ:150355677 MSN:decode360@hotmail.com

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 ::  :: 管理 ::
            397 隨筆 :: 33 文章 :: 29 評(píng)論 :: 0 Trackbacks
          <2008年12月>
          30123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          公告

          話到七分,酒至微醺,筆墨疏宕,言詞婉約,古樸殘破,含蓄醞籍,就是不完而美之最高境界。

          常用鏈接

          留言簿(13)

          隨筆分類(397)

          隨筆檔案(397)

          文章分類(33)

          新聞分類(15)

          收藏夾(74)

          Blog_List

          IT_Web

          My_Link

          最新隨筆

          最新評(píng)論

          堆棧--Stack類學(xué)習(xí)
          ?
          ??? 最近學(xué)習(xí)數(shù)據(jù)結(jié)構(gòu)的時(shí)候,用java實(shí)現(xiàn)了一下堆棧,就是用stack這個(gè)類,在這里記錄一下學(xué)習(xí)過程。
          ?
          ?
          Class Stack<E>
          						
          								java.lang.Object
          						
          						
          						
          								java.util.AbstractCollection
          						
          						<E>
               └ 
          						
          								java.util.AbstractList
          						
          						<E>
                    └ 
          						
          								java.util.Vector
          						
          						<E>
                        └ java.util.Stack<E>
          				
          ?
          ?
          Method Summary
          boolean empty ()
          ????????? Tests if this stack is empty.
          ? E peek ()
          ????????? Looks at the object at the top of this stack without removing it from the stack.
          ? E pop ()
          ????????? Removes the object at the top of this stack and returns that object as the value of this function.
          ? E push ( E item)
          ????????? Pushes an item onto the top of this stack.
          int search ( Object o)
          ????????? Returns the 1-based position where an object is on this stack.
          ?
          ?
          ?
          另外還繼承了java.util.Vector<E>中的基本方法,常見的用法如下所示:
          ?
          ?
          import java.util.*;
          ?
          public class kkk{
          public static void main(String args[]){
          ? int m = 1;
          ? Stack<Integer> a = new Stack<Integer>();
          ?
          ? a.push(m);?? //壓入1
          ? a.push(m+1); //壓入2
          ? a.push(m+2); //壓入3
          ? a.push(m+3); //壓入4
          ? a.push(m+4); //壓入5
          ??? System.out.println(a.push(m+5)); //壓入6,并返回當(dāng)前值
          ?
          ??? System.out.println(a.empty());?? //判斷棧是否為空,為空則TRUE
          ??? System.out.println(a.isEmpty()); //判斷棧是否為空,為空則TRUE
          ???
          ? System.out.println(a.size());???? //返回當(dāng)前棧長(zhǎng)度
          ? System.out.println(a.toString()); //返回當(dāng)前棧的內(nèi)容
          ?
          ? System.out.println(a.lastElement());? //返回當(dāng)前棧中的最后一個(gè)元素6
          ? System.out.println(a.firstElement()); //返回當(dāng)前棧中的第一個(gè)元素1
          ?
          ? System.out.println(a.peek()); //返回當(dāng)前值6
          ? System.out.println(a.pop());? //返回當(dāng)前值并壓出6
          ? System.out.println(a.pop());? //返回當(dāng)前值并壓出5
          ?
          ? System.out.println(a.search(3));
          ? //搜索“3”在棧中出現(xiàn)的位置,頂端為1,其余依次累加,若有多個(gè)則返回第1個(gè)的位置
          ?
          ? a.add(m+9); //壓入10,跟push一樣的效果
          ? a.clear(); //清空棧
          ?
          ??? System.out.println(a.empty());?? //判斷棧是否為空,為空則TRUE
          ??? System.out.println(a.isEmpty()); //判斷棧是否為空,為空則TRUE
          ?
          }
          }
          ?
          ?
          ?
          posted on 2008-12-01 22:42 decode360 閱讀(183) 評(píng)論(0)  編輯  收藏 所屬分類: 04.Java

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 南投县| 穆棱市| 秦安县| 绥中县| 蓬安县| 堆龙德庆县| 休宁县| 广宗县| 河池市| 沙田区| 湖口县| 望城县| 永兴县| 宜川县| 霸州市| 左云县| 建平县| 静乐县| 刚察县| 杭锦旗| 收藏| 惠水县| 仙桃市| 蚌埠市| 清涧县| 济宁市| 乌兰县| 佛学| 固镇县| 内江市| 平乡县| 固始县| 斗六市| 建昌县| 天台县| 大余县| 民乐县| 富平县| 如皋市| 杭州市| 阿城市|