Java Stack - addElement() Method
The java.util.Stack.addElement() method is used to add a new element at the top of the stack. Addition of new element results into increasing the stack size by one. This method has same effect as push method.
Syntax
public void addElement (E element)
Here, E is the type of element maintained by the container.
Parameters
element |
Specify the element which need to be added in the stack. |
Return Value
void type.
Exception
NA
Example:
In the example below, the java.util.Stack.addElement() method is used to add a new element at the top of the stack called MyStack.
import java.util.*; public class MyClass { public static void main(String[] args) { //creating a stack Stack<Integer> MyStack = new Stack<Integer>(); //addElementulating stack MyStack.addElement(10); MyStack.addElement(20); MyStack.addElement(30); //printing stack System.out.println("MyStack contains: " + MyStack); } }
The output of the above code will be:
MyStack contains: [10, 20, 30]
❮ Java.util - Stack