java offer_Java ArrayDeque offer()方法与示例

java offer

ArrayDeque类的offer()方法 (ArrayDeque Class offer() method)

  • offer() Method is available in java.lang package.

    offer()方法在java.lang包中可用。

  • offer() Method is used to add the given element at the end of the deque.

    offer()方法用于在双端队列的末尾添加给定元素。

  • offer() Method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    offer()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名访问该方法,则会收到错误消息。

  • offer() Method may throw an exception at the time of adding element in the deque.

    offer()方法可能会在双端队列中添加元素时引发异常。

    NullPointerException: This exception may throw when the given element is null exists.

    NullPointerException :当给定元素为null时,可能引发此异常。

Syntax:

句法:

    public boolean offer(T element);

Parameter(s):

参数:

  • T element – represents the element to be inserted at the last of this deque.

    T element –表示要在此双端队列的最后插入的元素。

Return value:

返回值:

The return type of the method is boolean, it returns true if the given element is added last into the deque successfully, else it returns false.

该方法的返回类型是布尔值 ,如果给定元素最后加入到双端队列成功返回true,否则返回false。

Example:

例:

// Java program to demonstrate the example 
// of boolean offer(T element) method of ArrayDeque 

import java.util.*;

public class OfferOfArrayDeque {
    public static void main(String[] args) {
        // Creating an ArrayDeque with initial capacity of
        // storing elements
        Deque < String > d_queue = new ArrayDeque < String > (10);

        // By using add() method to add elements
        // in ArrayDeque
        d_queue.add("C");
        d_queue.add("C++");
        d_queue.add("Java");
        d_queue.add("Php");
        d_queue.add("DotNet");

        System.out.println();

        // Display Deque Elements
        System.out.println("d_queue before offer(): ");
        System.out.println("ArrayDeque Elements = " + d_queue);

        System.out.println();

        // By using offer() method to add the
        // elements at the last in ArrayDeque
        d_queue.offer("Python");

        System.out.println("d_queue after offer(): ");
        // Display Deque Elements
        System.out.println("d_queue.offer() : " + d_queue);
    }
}

Output

输出量


d_queue before offer(): 
ArrayDeque Elements = [C, C++, Java, Php, DotNet]

d_queue after offer(): 
d_queue.offer() : [C, C++, Java, Php, DotNet, Python]


翻译自: https://www.includehelp.com/java/arraydeque-offer-method-with-example.aspx

java offer

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值