Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the ArrayDeque.addLast 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 a stack or queue. Null elements are prohibited.

What is the addLast method of the ArrayDeque?

The addLast method can be used to add an element to the end of the ArrayDeque object.

Syntax

public void addLast(E e);

This method takes the element to be added as an argument.

This method doesn’t return any value.

Code

The code below demonstrates how to use the addLast method.

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

Explanation

In the code above:

  • In line 1, we import the ArrayDeque class.

  • In line 4, we create an ArrayDeque object with the name arrayDeque.

  • In line 5, we use the addLast method of the arrayDeque object to add an element ("3") to the end of the deque and print it.

  • In line 8, we use the addLast method to add an element ("2") to the end of the deque and print it.

  • In line 11, we use the addLast method to add an element ("1") to the end of the deque.

RELATED TAGS

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