The
addElement(E) method of
Stack Class is used to append the element passed as a parameter to this function at the end of the Stack.
Syntax:
boolean addElement(E obj)
Here, E is the type of elements maintained
by this container.
Parameters: This function accepts a parameter
E obj which is the object to be added at the end of the Stack.
Return Value: The method returns
True if at least one action of append is performed, else
False.
Below program illustrate the Java.util.Stack.addElement() method:
Example 1:
import java.util.*;
import java.util.ArrayList;
public class GFG {
public static void main(String args[])
{
Stack<String> stack = new Stack<String>();
stack.add("Geeks");
stack.add("for");
stack.add("Geeks");
stack.add("10");
stack.add("20");
System.out.println("The Stack is: " + stack);
stack.addElement("GeeksForGeeks");
System.out.println("The new Stack is: " + stack);
}
}
|
Output:
The Stack is: [Geeks, for, Geeks, 10, 20]
The new Stack is: [Geeks, for, Geeks, 10, 20, GeeksForGeeks]
Example 2:
import java.util.*;
public class StackDemo {
public static void main(String args[])
{
Stack<Integer> stack
= new Stack<Integer>();
stack.add(10);
stack.add(20);
stack.add(30);
stack.add(40);
stack.add(50);
System.out.println("The Stack is: " + stack);
stack.addElement(100);
System.out.println("The new Stack is: " + stack);
}
}
|
Output:
The Stack is: [10, 20, 30, 40, 50]
The new Stack is: [10, 20, 30, 40, 50, 100]
Last Updated :
24 Dec, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...