队列接口的offer(E e)方法在不违反容量限制的情况下可以立即将指定的元素插入此队列。此方法优于add()方法,因为在容器的容量已满时,此方法不会引发异常,因为它会返回false。

用法:

boolean offer(E e)

参数:此方法接受强制参数e,该参数是要插入队列中的元素。

返回:成功插入时此方法返回true,否则返回false。

异常:该函数引发四个异常,如下所述:

ClassCastException:当要输入的元素的类阻止将其添加到此容器中时:

IllegalArgumentException:当元素的某些属性阻止将其添加到队列中时:

NullPointerException:当要插入的元素作为null传递并且Queue的接口不允许使用null元素时。

以下示例程序旨在说明offer()队列方法:

程序1:借助LinkedBlockingDeque。

// Java Program Demonstrate offer()

// method of Queue

import java.util.*;

import java.util.concurrent.LinkedBlockingQueue;

public class GFG {

public static void main(String[] args)

throws IllegalStateException

{

// create object of Queue

Queue Q

= new LinkedBlockingQueue(3);

if (Q.offer(10))

System.out.println("The Queue is not full"

+ " and 10 is inserted");

else

System.out.println("The Queue is full");

if (Q.offer(15))

System.out.println("The Queue is not full"

+ " and 15 is inserted");

else

System.out.println("The Queue is full");

if (Q.offer(25))

System.out.println("The Queue is not full"

+ " and 25 is inserted");

else

System.out.println("The Queue is full");

if (Q.offer(20))

System.out.println("The Queue is not full"

+ " and 20 is inserted");

else

System.out.println("The Queue is full");

// before removing print Queue

System.out.println("Queue:" + Q);

}

}

输出:

The Queue is not full and 10 is inserted

The Queue is not full and 15 is inserted

The Queue is not full and 25 is inserted

The Queue is full

Queue:[10, 15, 25]

程序2:借助ConcurrentLinkedDeque。

// Java Program Demonstrate offer()

// method of Queue

import java.util.*;

import java.util.concurrent.ConcurrentLinkedDeque;

public class GFG {

public static void main(String[] args)

throws IllegalStateException

{

// create object of Queue

Queue Q

= new ConcurrentLinkedDeque();

if (Q.offer(10))

System.out.println("The Queue is not full"

+ " and 10 is inserted");

else

System.out.println("The Queue is full");

if (Q.offer(15))

System.out.println("The Queue is not full"

+ " and 15 is inserted");

else

System.out.println("The Queue is full");

if (Q.offer(25))

System.out.println("The Queue is not full"

+ " and 25 is inserted");

else

System.out.println("The Queue is full");

if (Q.offer(20))

System.out.println("The Queue is not full"

+ " and 20 is inserted");

else

System.out.println("The Queue is full");

// before removing print Queue

System.out.println("Queue:" + Q);

}

}

输出:

The Queue is not full and 10 is inserted

The Queue is not full and 15 is inserted

The Queue is not full and 25 is inserted

The Queue is not full and 20 is inserted

Queue:[10, 15, 25, 20]

程序3:借助ArrayDeque。

// Java Program Demonstrate offer()

// method of Queue

import java.util.*;

public class GFG {

public static void main(String[] args)

throws IllegalStateException

{

// create object of Queue

Queue Q

= new ArrayDeque(6);

if (Q.offer(10))

System.out.println("The Queue is not full"

+ " and 10 is inserted");

else

System.out.println("The Queue is full");

if (Q.offer(15))

System.out.println("The Queue is not full"

+ " and 15 is inserted");

else

System.out.println("The Queue is full");

if (Q.offer(25))

System.out.println("The Queue is not full"

+ " and 25 is inserted");

else

System.out.println("The Queue is full");

if (Q.offer(20))

System.out.println("The Queue is not full"

+ " and 20 is inserted");

else

System.out.println("The Queue is full");

// before removing print Queue

System.out.println("Queue:" + Q);

}

}

输出:

The Queue is not full and 10 is inserted

The Queue is not full and 15 is inserted

The Queue is not full and 25 is inserted

The Queue is not full and 20 is inserted

Queue:[10, 15, 25, 20]

程序4:借助LinkedList。

// Java Program Demonstrate offer()

// method of Queue

import java.util.*;

public class GFG {

public static void main(String[] args)

throws IllegalStateException

{

// create object of Queue

Queue Q

= new LinkedList();

if (Q.offer(10))

System.out.println("The Queue is not full"

+ " and 10 is inserted");

else

System.out.println("The Queue is full");

if (Q.offer(15))

System.out.println("The Queue is not full"

+ " and 15 is inserted");

else

System.out.println("The Queue is full");

if (Q.offer(25))

System.out.println("The Queue is not full"

+ " and 25 is inserted");

else

System.out.println("The Queue is full");

if (Q.offer(20))

System.out.println("The Queue is not full"

+ " and 20 is inserted");

else

System.out.println("The Queue is full");

// before removing print Queue

System.out.println("Queue:" + Q);

}

}

输出:

The Queue is not full and 10 is inserted

The Queue is not full and 15 is inserted

The Queue is not full and 25 is inserted

The Queue is not full and 20 is inserted

Queue:[10, 15, 25, 20]

以下示例程序旨在说明此方法引发的异常:

程序5:显示NullPointerException。

// Java Program Demonstrate offer()

// method of Queue when Null is passed

import java.util.*;

import java.util.concurrent.LinkedBlockingQueue;

public class GFG {

public static void main(String[] args)

throws NullPointerException

{

// create object of Queue

Queue Q

= new LinkedBlockingQueue();

// Add numbers to end of Deque

Q.offer(7855642);

Q.offer(35658786);

Q.offer(5278367);

try {

// when null is inserted

Q.offer(null);

}

catch (Exception e) {

System.out.println("Exception:" + e);

}

}

}

输出:

Exception:java.lang.NullPointerException

注意:其他两个异常是内部的,它们是由编译器引起的,因此无法在代码中显示。

java queue 使用,Java Queue offer()用法及代码示例相关推荐

  1. java priorityqueue_Java PriorityQueue offer()用法及代码示例

    java.util.PriorityQueue.offer()方法用于将特定元素插入优先级队列.它的行为类似于优先级队列的add()方法. 用法: Priority_Queue.offer(Objec ...

  2. java实现stack search_Java Stack search()用法及代码示例

    Java中的java.util.Stack.search(Object element)方法用于搜索堆栈中的元素并获取其与顶部的距离.此方法从1开始而不是从0开始计数位置.位于堆栈顶部的元素被视为在位 ...

  3. java range对象_Java LocalTime range()用法及代码示例

    LocalTime类的range()方法用于获取最小值和最大值形式的字段范围,并将该字段作为参数传递给此方法.此方法的返回值是该字段的ValueRange对象,并且该方法仅对LocalTime对象支持 ...

  4. java range(10)_Java Year range()用法及代码示例

    Year类的range()方法用于获取最大值和最小值的字段范围,并将该字段作为参数传递给此方法.此方法的返回值是该字段的ValueRange对象,并且该方法仅对Year对象支持的那些字段返回Value ...

  5. java dictionary 实例化_Java Dictionary put()用法及代码示例

    字典的put()方法用于在字典中插入映射.这意味着可以将特定键及其值映射到特定字典中. 用法: DICTIONARY.put(key, value) 参数:该方法有两个参数,都属于Dictionary ...

  6. java中的getfirst_Java LinkedList getFirst()用法及代码示例

    Java.util.LinkedList.getFirst()方法用于从LinkedList或列表开头的元素中获取或检索第一个元素. 用法: LinkedList.getFirst() 参数:此方法不 ...

  7. java byte转bigdecimal_Java BigDecimal byteValueExact()用法及代码示例

    java.math.BigDecimal.byteValueExact()是一个内置函数,它将BigDecimal转换为字节并检查丢失的信息.任何大于127或小于-128的BigDecimal值都将生 ...

  8. java collator_Java Collator compare(String, String)用法及代码示例

    java.text.Collat​​or类的compare()方法用于比较两个字符串的强度,并根据结果返回0,正值和负值作为输出. 用法: public abstract int compare(St ...

  9. java 的 provider_Java Provider.Service getProvider()用法及代码示例

    java.security.Provider.Service类的getProvider()方法用于返回此提供程序服务对象的提供程序. 用法: public final Provider getProv ...

最新文章

  1. 入门到放弃node系列之网络模块(二)
  2. WINCE的内存配置
  3. TensorFlow学习笔记(二):快速理解Tutorial第一个例子-MNIST机器学习入门 标签: 机器学习SoftmaxTensorFlow教程 2016-08-02 22:12 3729人阅
  4. Modelsim se仿真Xilinx IPcore
  5. 2021超详细的Dart语言基础总结~你值得拥有~
  6. Redis从入门到精通,至少要看看这篇!
  7. mysql 5.7.4 m14_win7 64位下如何安装配置mysql-5.7.4-m14-winx64(安装记录)
  8. Java-变量、常量
  9. Apache构建虚拟web主机
  10. 基于LSTM+Attention机制的IMDB影评数据分类学习实践
  11. Csharp: Searching Within a String
  12. Qt 实现多语言 国际化 翻译
  13. matlab 求反余弦,matlab中反余弦函数
  14. 华为5.0系统如何不用ROOT激活XPOSED框架的步骤
  15. 【数据结构】二叉树 —— 遍历二叉树 + 递归的分治(链式存储)
  16. MacBook将大文件分割成很多个小文件split命
  17. HTML如何设置四边形,css实现三角形和平形四边形
  18. USB-WIFI RTL8188CU模块驱动
  19. ASP.NET MVC 音乐商店 - 10. 完成导航和站点的设计
  20. Chrome(谷歌)浏览器调试教程珍藏版

热门文章

  1. 步行速度快慢测试软件,神奇!走路速度竟能测算你的寿命
  2. 美通社日历 | 媒体关注、会展信息、企业财报发布,节假日备忘(2月1日—2月7日)...
  3. 8.5 Python 实例14-第三方库自动安装脚本
  4. ts+vue3项目的echarts二次封装
  5. iOS调用系统相册显示英文
  6. org.springframework.beans.factory.BeanCurrentlyInCreationException错误
  7. jspxcms 4.0 mysql 5.0_Jspxcms 安装包
  8. SpringCloud学习一(回顾之前学的微服务知识点、springcloud入门概述、服务提供者和消费者)
  9. List遍历是否出现空指针
  10. Python|详解Python中的axis参数