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

队列接口的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

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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值