Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the ArrayDeque.size 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.

Answers Code

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 size method of ArrayDeque?

The size method can be used to get the number of elements present in the ArrayDeque object.

Syntax

public int size()

Parameters

This method doesn’t take any parameters.

Return value

This method returns an integer value that represents the number of elements present in the deque object.

Code

The code below demonstrates how to use the size method.

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

Explanation

In the above code:

  • In line 1: We import the ArrayDeque class.

  • In line 4: We create an ArrayDeque object with the name arrayDeque.

  • From lines 5 to 7: We use the add() method of the arrayDeque object to add three elements ("1","2","3") to deque.

  • In line 10: We use the size() method of the arrayDeque object to get the number of elements present in the deque. In our case, 3 will be returned.

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.

Answers Code
Keep Exploring