🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this guide, you will learn about the ArrayDeque push() method in Java programming and how to use it with an example.
1. ArrayDeque push() Method Overview
Definition:
The push() method of the ArrayDeque class in Java is used to push an element onto the stack represented by the ArrayDeque. This method inserts the element at the front of the ArrayDeque.
Syntax:
public void push(E e)
Parameters:
- e: The element to add.
Key Points:
- The push() method inserts the specified element at the front of the ArrayDeque.
- This method is equivalent to addFirst(E).
- The ArrayDeque class is a resizable array implementation of the Deque interface.
- ArrayDeque does not support the addition of null elements. Attempting to add null to an ArrayDeque will result in a NullPointerException.
- The ArrayDeque class is not thread-safe, so external synchronization is necessary if an ArrayDeque instance will be accessed by multiple threads concurrently.
2. ArrayDeque push() Method Example
import java.util.ArrayDeque;
public class ArrayDequeExample {
public static void main(String[] args) {
ArrayDeque<Integer> deque = new ArrayDeque<>();
// Adding elements using add() method
deque.add(1);
deque.add(2);
deque.add(3);
// Printing the original ArrayDeque
System.out.println("Original ArrayDeque: " + deque);
// Using push() to add an element at the front
deque.push(0);
// Printing the ArrayDeque after push operation
System.out.println("ArrayDeque after push: " + deque);
// Trying to push null to the ArrayDeque will throw NullPointerException
try {
deque.push(null);
} catch (NullPointerException e) {
System.out.println("Cannot push null to an ArrayDeque.");
}
}
}
Output:
Original ArrayDeque: [1, 2, 3] ArrayDeque after push: [0, 1, 2, 3] Cannot push null to an ArrayDeque.
Explanation:
In this example, we initialize an ArrayDeque and add some elements using the add() method.
We then use the push() method to insert the element 0 at the front of the ArrayDeque. The state of the ArrayDeque is printed before and after the push() operation.
Finally, we demonstrate that trying to push null to the ArrayDeque results in a NullPointerException.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment