Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

arraydeque
java
communitycreator

What is the ArrayDeque.offerLast method in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

An ArrayDequeArray Double Ended Queue is a growable array that allows us to add or remove an element from both the front and back. This class is the implementation class of the Deque interface.

ArrayDeque can be used as both a stack or queue. Null elements are prohibited.

What is the offerLast method?

The offerLast method of the ArrayDeque class can be used to add the element to the end of the ArrayDeque object.

Syntax

public boolean offerLast(E e)

Parameters

The offerLast method takes the element to be added to the deque as a parameter. This method throws NullPointerException if the argument is null.

Return value

The offerLast method returns true if the element is added to the deque. Otherwise, it returns false.

Code

The code below demonstrates how to use the offerLast method.

import java.util.ArrayDeque;
class OfferLast {
    public static void main( String args[] ) {
      ArrayDeque<String> arrayDeque = new ArrayDeque<>();
      arrayDeque.offerLast("1");
      System.out.println("The arrayDeque is " + arrayDeque);

      arrayDeque.offerLast("2");
      System.out.println("The arrayDeque is " + arrayDeque);

    }
}

Explanation

In the code above:

  • In line 1, we imported the ArrayDeque class.

  • In line 4, we created a ArrayDeque object with the name arrayDeque.

  • In line 5, we used the offerLast method of the arrayDeque object to add an element ("1") to the end of the deque. Then, we printed it.

  • In line 8, we used the offerLast method to add an element ("2") to the end of the deque. Then, we printed it.

RELATED TAGS

arraydeque
java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring