溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          雪山飛鵠

          溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks

          公告

          Locations of visitors to this page

          常用鏈接

          留言簿(18)

          隨筆分類(222)

          隨筆檔案(214)

          文章檔案(1)

          收藏夾(67)

          我的鏈接

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          package com.ipmotor.sm.db;

          import java.util.LinkedList;
          import java.util.Queue;
          import java.util.Stack;

          /**
           * 測試jdk中的棧和隊列
           * 
          @author scott
           *
           
          */
          public class TestQueueAndStack {
              
              
          /**
               * 測試隊列
               * <pre>
               * 隊列特點,先進先出,后進后出,火車過山洞例子
               * </pre>
               
          */
              
          static void testQueue(){
                  Queue
          <String> queue=new LinkedList<String>();
                  
          //添加幾個元素
                  queue.offer("a");
                  queue.offer(
          "b");
                  queue.offer(
          "c");
                  queue.offer(
          "d");
                  queue.offer(
          "e");
                  queue.add(
          "1");
                  queue.add(
          "2");
                  queue.add(
          "3");
                  queue.add(
          "4");
                  queue.add(
          "5");
                  System.out.println(
          "隊列中的元素是:"+queue);
                  
          //彈出元素
                  queue.poll();
                  System.out.println(
          "隊列中的元素是:"+queue);
                  
          //查看隊列中首個元素,并不移除
                  String peek=queue.peek();
                  System.out.println(
          "查看隊列中首個元素,并不移除:"+peek);
                  System.out.println(
          "隊列中的元素是:"+queue);
              }
              
              
              
          /**
               * 測試棧
               * <pre>
               * 先進后出,后進先出,水桶倒水
               * </pre>
               
          */
              
          static void testStack(){
                  Stack
          <String> stack=new Stack<String>();
                  
          //添加幾個元素
                  stack.push("a");
                  stack.push(
          "b");
                  stack.push(
          "c");
                  stack.push(
          "d");
                  stack.push(
          "e");
                  stack.add(
          "1");
                  stack.add(
          "2");
                  stack.add(
          "3");
                  stack.add(
          "4");
                  stack.add(
          "5");
                  System.out.println(
          "棧中的元素是:"+stack);
                  
          //彈出元素
                  stack.pop();
                  System.out.println(
          "棧中的元素是:"+stack);
                  
          //查看棧中首個元素,并不移除
                  String peek=stack.peek();
                  System.out.println(
          "查看棧中首個元素,并不移除:"+peek);
                  System.out.println(
          "棧中的元素是:"+stack);
              }
              

              
          /**
               * 
          @param args
               
          */
              
          public static void main(String[] args) {
                  testQueue();
                  System.out.println(
          "-------棧--------");
                  testStack();
              }

          }

          隊列中的元素是:[a, b, c, d, e, 1, 2, 3, 4, 5]
          隊列中的元素是:[b, c, d, e, 1, 2, 3, 4, 5]
          查看隊列中首個元素,并不移除:b
          隊列中的元素是:[b, c, d, e, 1, 2, 3, 4, 5]
          -------棧--------
          棧中的元素是:[a, b, c, d, e, 1, 2, 3, 4, 5]
          棧中的元素是:[a, b, c, d, e, 1, 2, 3, 4]
          查看棧中首個元素,并不移除:4
          棧中的元素是:[a, b, c, d, e, 1, 2, 3, 4]
          posted on 2011-07-08 13:37 雪山飛鵠 閱讀(958) 評論(0)  編輯  收藏 所屬分類: javase
          主站蜘蛛池模板: 罗定市| 烟台市| 安溪县| 木里| 新干县| 安仁县| 宣汉县| 大余县| 平山县| 广安市| 铜鼓县| 西充县| 额济纳旗| 阿克陶县| 桐柏县| 米林县| 清原| 石楼县| 宜春市| 那坡县| 渭源县| 旬邑县| 乡城县| 绿春县| 同德县| 淅川县| 平山县| 米易县| 浦东新区| 高邑县| 双流县| 乌拉特中旗| 周口市| 张掖市| 许昌市| 泾阳县| 绥中县| 莲花县| 辰溪县| 观塘区| 晋城|