zoukankan      html  css  js  c++  java
  • Java 从入门到进阶之路(二十六)

    在之前的文章我们介绍了一下 Java 中的  集合框架中的Collection 的子接口 List,本章我们来看一下 Java 集合框架中的Collection 的子接口 Queue。

    在之前我们讲 List 和 Set 的时候可以通过下标的形式获取想要的元素,在 Collection 中其实还有 Queue 这个子接口,就是队列的意思。

    队列可以形象的比喻为在火车站排队买票,先进先出,后进后出,接下来我们就用代码来实现一下:

     1 import java.util.LinkedList;
     2 import java.util.Queue;
     3 
     4 /**
     5  * java.util.Queue
     6  * 队列
     7  * 队列也可以存放一组元素,但是存取元素必须
     8  * 遵循:先进先出原则
     9  */
    10 
    11 public class Main {
    12     public static void main(String[] args) {
    13         /**
    14          * LinkedList 也实现了队列接口,
    15          * 因为它可以保存一组元素
    16          * 并且首尾增删快,正好符合队列的特点
    17          */
    18         Queue<String> queue = new LinkedList<String>();
    19         /**
    20          * boolean offer(E e)
    21          * 入队操作,向队尾追加一个新元素
    22          */
    23         queue.offer("one");
    24         queue.offer("two");
    25         queue.offer("three");
    26         queue.offer("four");
    27         System.out.println(queue.size()); // 4
    28         System.out.println(queue); // [one, two, three, four]
    29 
    30         /**
    31          * E peek()
    32          * 引用队首元素,但是不做出队操作
    33          * 返回值是该元素
    34          */
    35         String peek = queue.peek();
    36         System.out.println(peek); // one
    37         System.out.println(queue); // [one, two, three, four]
    38 
    39         /**
    40          * E poll()
    41          * 出队操作,从队首获取元素,获取后该元素
    42          * 就从队列中被删除
    43          * 返回值是该元素
    44          */
    45         String old = queue.poll();
    46         System.out.println(old); // one
    47         System.out.println(queue); // [two, three, four]
    48 
    49         /**
    50          * 循环获取每个元素
    51          * 不能通过 i++ 的形式,因为每次取出一个后 size 会有变化
    52          */
    53         for (int i = queue.size(); i > 0; i--) {
    54             System.out.println(queue.poll()); // two three four
    55         }
    56         // 用 while 更好点
    57         while (queue.size() > 0) {
    58             System.out.println(queue.poll()); // two three four
    59         }
    60     }
    61 }

    队列在编程中一般不会用到,除非有特殊规定需要先后顺序的时候采用,例如在玩游戏服务器爆满要排队进入游戏的时候,当然得排除会员插队现象。

    下面我们再来看一下另一个概念:栈

    栈:存储一组元素,但是存取元素必须遵循先进后出的原则,通常为了实现后退这类功能会使用栈。

    在现实生活中子弹上膛打出就是一个很经典的栈。

     在上图中是队列的一些方法,如果我们把右边的方法去掉不用,仅左边的方法就是一个栈,先进后出。

    在栈中还有自己的方法 push 和 pop,具体操作如下:

     1 import java.util.Deque;
     2 import java.util.LinkedList;
     3 
     4 public class Main {
     5     public static void main(String[] args) {
     6         /**
     7          * Java.util.Deque
     8          * 双端队列,两端都可以进出队
     9          * 当调用从一端进出队列操作时,就形成了栈接口
    10          * 因此,双端队列为栈提供了两个方法
    11          * push,pop
    12          */
    13         Deque<String> stack = new LinkedList<String>();
    14         stack.push("one");
    15         stack.push("two");
    16         stack.push("three");
    17         stack.push("four");
    18         System.out.println(stack.size()); // 4
    19         System.out.println(stack); // [four, three, two, one]
    20 
    21         /**
    22          * 获取第一个元素
    23          */
    24         String peek = stack.peek();
    25         System.out.println(peek); // four
    26         System.out.println(stack); // [four, three, two, one]
    27 
    28         /**
    29          * 出栈操作
    30          */
    31         String old = stack.pop();
    32         System.out.println(old); // four
    33         System.out.println(stack); // [three, two, one]
    34 
    35         /**
    36          * 循环出栈
    37          */
    38         while (stack.size() > 0) {
    39             System.out.println(stack.pop()); // three two one
    40         }
    41     }
    42 }

    队列和栈的区别在于一个先进先出,一个先进后出,很多方法都是通用的,需要自己在实际开发应用中使用。

  • 相关阅读:
    【零基础】量子纠缠图像问世,简单解读实验原理
    【零基础】一文读懂CPU(从二极管到超大规模集成电路)
    【零基础】搞定LAMP(linux、apache、mysql、php)环境安装图文教程(基于centos7)
    【零基础】Selenium:Webdriver图文入门教程java篇(附相关包下载)
    【零基础】快速入门爬虫框架HtmlUnit
    【零基础】speech driven animation中文安装使用指南
    【零基础】斯坦福四足机器人DIY指引
    【零基础】为什么Facebook发币就不一样
    【零基础】彻底搞懂51单片机各种型号(ATMEL系列)
    【零基础】简单说说一键果体APP的AI
  • 原文地址:https://www.cnblogs.com/weijiutao/p/12072734.html
Copyright © 2011-2022 走看看