Class Stack
The Stack class represents a last-in-first-out (LIFO) stack of objects. It extends class Vector with five operations that allow a vector to be treated as a stack. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack, a method to test for whether the stack is empty , and a method to search the stack for an item and discover how far it is from the top.
When a stack is first created, it contains no items.
A more complete and consistent set of LIFO stack operations is provided by the Deque interface and its implementations, which should be used in preference to this class. For example:
Deque stack = new ArrayDeque();
- Field Summary
- Fields declared in class java.util.Vector
- Fields declared in class java.util.AbstractList
- Constructor Summary
- Method Summary
- Methods declared in class java.util.Vector
- Methods declared in class java.lang.Object
- Methods declared in interface java.util.Collection
- Methods declared in interface java.util.List
- Constructor Details
- Stack
- Method Details
- push
- pop
- peek
- empty
- search
- Push method in java
- Field Summary
- Fields declared in class java.util.Vector
- Fields declared in class java.util.AbstractList
- Constructor Summary
- Method Summary
- Methods declared in class java.util.Vector
- Methods declared in class java.lang.Object
- Methods declared in interface java.util.Collection
- Methods declared in interface java.util.List
- Constructor Detail
- Stack
- Method Detail
- push
- pop
- peek
- empty
- search
- Field Summary
- Fields declared in class java.util.Vector
- Fields declared in class java.util.AbstractList
- Constructor Summary
- Method Summary
- Methods declared in class java.util.Vector
- Methods declared in class java.lang.Object
- Methods declared in interface java.util.Collection
- Methods declared in interface java.util.List
- Constructor Details
- Stack
- Method Details
- push
- pop
- peek
- empty
- search
- Java array.push
- Syntax and Parameters
- 1. Stack
- 2. LinkedListObject
- 3. ArrayDeque
- How does the push method work in Java?
- Examples to Implement Java array.push
- 1. Java program to push elements of string type in a stack.
- 2. Java program to push elements of integer type in LinkedList.
- 3. Java program to add integer elements to stack.
- 4. Java program to add string elements to LinkedList.
- 5. Java program to add integer elements to Deque.
- Recommended Articles
Field Summary
Fields declared in class java.util.Vector
Fields declared in class java.util.AbstractList
Constructor Summary
Method Summary
Methods declared in class java.util.Vector
Methods declared in class java.lang.Object
Methods declared in interface java.util.Collection
Methods declared in interface java.util.List
Constructor Details
Stack
Method Details
push
pop
peek
empty
search
Returns the 1-based position where an object is on this stack. If the object o occurs as an item in this stack, this method returns the distance from the top of the stack of the occurrence nearest the top of the stack; the topmost item on the stack is considered to be at distance 1 . The equals method is used to compare o to the items in this stack.
Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.
Push method in java
The Stack class represents a last-in-first-out (LIFO) stack of objects. It extends class Vector with five operations that allow a vector to be treated as a stack. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack, a method to test for whether the stack is empty , and a method to search the stack for an item and discover how far it is from the top. When a stack is first created, it contains no items. A more complete and consistent set of LIFO stack operations is provided by the Deque interface and its implementations, which should be used in preference to this class. For example:
Deque stack = new ArrayDeque();
Field Summary
Fields declared in class java.util.Vector
Fields declared in class java.util.AbstractList
Constructor Summary
Method Summary
Methods declared in class java.util.Vector
Methods declared in class java.lang.Object
Methods declared in interface java.util.Collection
Methods declared in interface java.util.List
Constructor Detail
Stack
Method Detail
push
pop
peek
empty
search
Returns the 1-based position where an object is on this stack. If the object o occurs as an item in this stack, this method returns the distance from the top of the stack of the occurrence nearest the top of the stack; the topmost item on the stack is considered to be at distance 1 . The equals method is used to compare o to the items in this stack.
Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.
Class Stack
The Stack class represents a last-in-first-out (LIFO) stack of objects. It extends class Vector with five operations that allow a vector to be treated as a stack. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack, a method to test for whether the stack is empty , and a method to search the stack for an item and discover how far it is from the top.
When a stack is first created, it contains no items.
A more complete and consistent set of LIFO stack operations is provided by the Deque interface and its implementations, which should be used in preference to this class. For example:
Deque stack = new ArrayDeque();
Field Summary
Fields declared in class java.util.Vector
Fields declared in class java.util.AbstractList
Constructor Summary
Method Summary
Methods declared in class java.util.Vector
Methods declared in class java.lang.Object
Methods declared in interface java.util.Collection
Methods declared in interface java.util.List
Constructor Details
Stack
Method Details
push
pop
peek
empty
search
Returns the 1-based position where an object is on this stack. If the object o occurs as an item in this stack, this method returns the distance from the top of the stack of the occurrence nearest the top of the stack; the topmost item on the stack is considered to be at distance 1 . The equals method is used to compare o to the items in this stack.
Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.
Java array.push
In Java, push is a method that adds elements in the stack, array, LinkedList, etc. In Java, you can add an element to the stack using the push(E el) method from the java.util.Stack class. In the case of LinkedList, Java.util.LinkedList. It functions similarly to the addFirst() method in LinkedList. Java.util.ArrayDeque.push(E el) method in ArrayDeque pushes the element into the top of the Deque.
Web development, programming languages, Software testing & others
Syntax and Parameters
Let us see the syntax of the push method in a stack, LinkedList, and Deque.
1. Stack
- Parameters: Item el of the element type that has to be pushed to the stack.
- Return Value: The argument passed will be returned.
2. LinkedListObject
LinkedListObject.push(Object el)
- Parameters: Item el of an object type that has to be pushed to the stack.
- Return Type: No value will be returned.
3. ArrayDeque
- Parameters: Item el of ArrayDeque type that has to be pushed to the stack.
- Return Value: No value will be returned.
How does the push method work in Java?
The push method works similarly in Stack, LinkedList, and Deque. To achieve the desired result, you can follow these steps:
1. Create a stack, LinkedList, or Deque based on the requirement.
Stacks = new Stack(); LinkedListli = new LinkedList<>(); Dequedq = new ArrayDeque(8);
2. add elements to it using the push method.
Examples to Implement Java array.push
Below are examples of implementing Java array.push:
1. Java program to push elements of string type in a stack.
import java.util.*; public class PushMethodExample < public static void main(String args[]) < // create a stack Stacks = new Stack(); s.push("Happy"); s.push("Sad"); s.push("Confused"); s.push("Tensed"); s.push("Mixed Emotions"); // Print elements in stack System.out.println("Stack Elements: " + s); // Push new elements s.push("Emotions"); s.push("Exists"); // Stack after adding new elements System.out.println("Stack after adding new elements " + s); > >
Explanation: First, create a stack s and add elements using the push() method. After that, you can print the stack and add the elements again to verify if they are successfully inserted. When you execute the code, you will be able to display the stack both before and after adding the new elements.
2. Java program to push elements of integer type in LinkedList.
import java.util.*; public class PushMethodExample < public static void main(String args[]) < // create a LinkedList LinkedListli = new LinkedList<>(); li.push(45); li.push(90); li.push(67); li.push(33); li.push(56); // Print elements in LinkedList System.out.println("LinkedList Elements: " + li); // Push new elements li.push(456); li.push(678); // LinkedList after adding new elements System.out.println("LinkedList after adding new elements " + li); > >
Explanation: Create a LinkedList li and add elements of integer type using the push() method. Then, display the LinkedList and add the items again so that it can be determined whether or not elements are inserted. On executing the code, LinkedList, before adding new elements and after adding new elements, can be displayed.
3. Java program to add integer elements to stack.
import java.util.*; public class PushMethodExample < public static void main(String args[]) < // create a stack Stacks = new Stack(); s.push(45); s.push(90); s.push(67); s.push(33); s.push(56); // Print elements in stack System.out.println("stack Elements: " + s); // Push new elements s.push(456); s.push(678); // stack after adding new elements System.out.println("stack after adding new elements " + s); > >
Explanation: First, we create a stack to accept integer elements. Then, we use the push() method to add elements to the stack. We print the current elements in the stack. Next, we add two new elements to the stack. To verify the successful addition of these elements, we print the elements in the stack again. When executing the code, you can observe that the stack does indeed contain the two additional elements that were added.
4. Java program to add string elements to LinkedList.
import java.util.*; public class PushMethodExample < public static void main(String args[]) < // create a LinkedList LinkedListli = new LinkedList<>(); li.push("Happy"); li.push("Sad"); li.push("Confused"); li.push("Tensed"); li.push("Mixed Emotions"); // Print elements in LinkedList System.out.println("LinkedList Elements: " + li ); // Push new elements li.push("Emotions"); li.push("Exists"); // LinkedList after adding new elements System.out.println("LinkedList after adding new elements " + li ); > >
Explanation: Create a LinkedList li and add elements of string type using the push() method. On executing the code, LinkedList, before adding new elements and after adding new elements, can be displayed.
5. Java program to add integer elements to Deque.
import java.util.ArrayDeque; import java.util.Deque; public class PushMethodExample < public static void main(String[] args) < // Create ArrayDeque Dequedq = new ArrayDeque(8); // Add elements to the deque using add() method dq.add(23); dq.add(56); dq.add(78); dq.add(13); // Print elements in deque System.out.println("Elements in deque are : "); for (Integer n : dq) < System.out.println("No : " + n); >// Add new elements using the method push() dq.push(456); dq.push(3432); // Print elements in Deque after inserting new elements System.out.println("Deque after inserting new elements:"); for (Integer n : dq) < System.out.println("No : " + n); >> >
Explanation: Create a deque for adding the elements. For that, use the method push() and add the elements of integer type. Then, print the Deque and identify the current elements present in it.
Recommended Articles
This is a guide to Java array.push. Here we discuss an introduction to Java array.push, syntax, parameters, and how it works with programming examples. You can also go through our other related articles to learn more –
89+ Hours of HD Videos
13 Courses
3 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
97+ Hours of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
JAVA Course Bundle — 78 Courses in 1 | 15 Mock Tests
416+ Hours of HD Videos
78 Courses
15 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.8