• 数据结构——java Queue类


    定义    


    队列是一种特殊的线性表,它只允许在表的前端进行删除操作,而在表的后端进行插入操作。

    LinkedList类实现了Queue接口,因此我们可以把LinkedList当成Queue来用

    图例            


     Queue本身是一种先入先出的模型(FIFO),和我们日常生活中的排队模型很类似。根据不同的实现,他们主要有数组和链表两种实现形式。如下图:

     

     

    与队列相关的类的关系图如下:

    常用方法              


    序号 方法名 描述
    1 boolean add(E e) 将指定的元素插入到队列中。
    2 Object element() 检索该队列的头。
      boolean offer(E e) 将指定元素插入到该队列中(在该队列容量之内)。
    3 Object peek() 检索该队列的头,如果该队列为空返回null。
    4 Object poll() 检索并删除该队列的头,如果该队列为空返回null。
    5 Object remove() 检索并删除该队列的头。

    源码


     1 package java.util;
     2 
     3 public interface Queue<E> extends Collection<E> {
     4     /**
     5      * Inserts the specified element into this queue if it is possible to do so
     6      * immediately without violating capacity restrictions, returning
     7      * {@code true} upon success and throwing an {@code IllegalStateException}
     8      * if no space is currently available.
     9      *
    10      * @param e the element to add
    11      * @return {@code true} (as specified by {@link Collection#add})
    12      * @throws IllegalStateException if the element cannot be added at this
    13      *         time due to capacity restrictions
    14      * @throws ClassCastException if the class of the specified element
    15      *         prevents it from being added to this queue
    16      * @throws NullPointerException if the specified element is null and
    17      *         this queue does not permit null elements
    18      * @throws IllegalArgumentException if some property of this element
    19      *         prevents it from being added to this queue
    20      */
    21     boolean add(E e);
    22 
    23     /**
    24      * Inserts the specified element into this queue if it is possible to do
    25      * so immediately without violating capacity restrictions.
    26      * When using a capacity-restricted queue, this method is generally
    27      * preferable to {@link #add}, which can fail to insert an element only
    28      * by throwing an exception.
    29      *
    30      * @param e the element to add
    31      * @return {@code true} if the element was added to this queue, else
    32      *         {@code false}
    33      * @throws ClassCastException if the class of the specified element
    34      *         prevents it from being added to this queue
    35      * @throws NullPointerException if the specified element is null and
    36      *         this queue does not permit null elements
    37      * @throws IllegalArgumentException if some property of this element
    38      *         prevents it from being added to this queue
    39      */
    40     boolean offer(E e);
    41 
    42     /**
    43      * Retrieves and removes the head of this queue.  This method differs
    44      * from {@link #poll poll} only in that it throws an exception if this
    45      * queue is empty.
    46      *
    47      * @return the head of this queue
    48      * @throws NoSuchElementException if this queue is empty
    49      */
    50     E remove();
    51 
    52     /**
    53      * Retrieves and removes the head of this queue,
    54      * or returns {@code null} if this queue is empty.
    55      *
    56      * @return the head of this queue, or {@code null} if this queue is empty
    57      */
    58     E poll();
    59 
    60     /**
    61      * Retrieves, but does not remove, the head of this queue.  This method
    62      * differs from {@link #peek peek} only in that it throws an exception
    63      * if this queue is empty.
    64      *
    65      * @return the head of this queue
    66      * @throws NoSuchElementException if this queue is empty
    67      */
    68     E element();
    69 
    70     /**
    71      * Retrieves, but does not remove, the head of this queue,
    72      * or returns {@code null} if this queue is empty.
    73      *
    74      * @return the head of this queue, or {@code null} if this queue is empty
    75      */
    76     E peek();
    77 }

    参考:http://blog.csdn.net/u010617952/article/details/51726789

  • 相关阅读:
    java中return在Try-Catch中的执行顺序
    面向对象软件开发方法概述
    内部类
    JAVA中的继承
    错题分析
    【cocos2d-x 手游研发小技巧(1)自定义制作怪物伤害数值】
    【cocos2d-x 手游研发----地图活起来了】
    【cocos2d-x 手游研发----精灵的八面玲珑】
    【cocos2d-x 手游研发----研发思路及感想】
    通俗易懂的讲解区块链
  • 原文地址:https://www.cnblogs.com/Y-zhiwei/p/8303199.html
Copyright © 2020-2023  润新知