zoukankan      html  css  js  c++  java
  • Java 队列的`add()`方法和`offer()`方法的区别

    查阅API文档,找到

    add():增加一个元素。如果队列已满,则抛出一个IIIegaISlabEepeplian异常

    Inserts the specified element at the tail of this queue. As the queue is unbounded, this method will never throw IllegalStateException or return false.
    

    offer():添加一个元素并返回true。如果队列已满,则返回false

    Inserts the specified element at the tail of this queue. As the queue is unbounded, this method will never return false.
    

    分析

    • 两者都是往队列尾部插入元素
    • 当超出队列界限的时候,add()方法是抛出异常让你处理,而offer()方法是直接返回false

    Java队列的部分调用方法

    方法 作用 说明
    add() 增加一个元素 如果队列已满,则抛出一个IIIegaISlabEepeplian异常
    remove() 移除并返回队列头部的元素 如果队列为空,则抛出一个NoSuchElementException异常
    element() 返回队列头部的元素 如果队列为空,则抛出一个NoSuchElementException异常
    offer() 添加一个元素并返回true 如果队列已满,则返回false
    poll() 移除并返问队列头部的元素 如果队列为空,则返回null
    peek() 返回队列头部的元素 如果队列为空,则返回null
    put() 添加一个元素 如果队列满,则阻塞
    take() 移除并返回队列头部的元素 如果队列为空,则阻塞

    关于Java队列更多详见:java队列——queue详细分析

  • 相关阅读:
    VMwareTools安装+CentOS分辨率调整
    WSDL文件示例及说明
    CentOS 5.4 服务器配置 yum安装Apache+php+Mysql+phpmyadmin
    linuxVi使用方法(备查)
    C++ 泛型算法定制操作
    C++ 迭代器分类
    求最长回文串的长度
    C++ 关联容器
    html标签之label
    转载div+css命名规范标准
  • 原文地址:https://www.cnblogs.com/melodyjerry/p/13018920.html
Copyright © 2011-2022 走看看