Stack elements() method in Java with Example
Last Updated :
24 Dec, 2018
The Java.util.Stack.elements() method of Stack class in Java is used to get the enumeration of the values present in the Stack.
Syntax:
Enumeration enu = Stack.elements()
Parameters: The method does not take any parameters.
Return value: The method returns an enumeration of the values of the Stack.
Below programs are used to illustrate the working of the java.util.Stack.elements() method:
Program 1:
import java.util.*;
public class Stack_Demo {
public static void main(String[] args)
{
Stack<String> stack = new Stack<String>();
stack.add( "Geeks" );
stack.add( "4" );
stack.add( "Geeks" );
stack.add( "Welcomes" );
stack.add( "You" );
System.out.println( "The Stack is: " + stack);
Enumeration enu = stack.elements();
System.out.println( "The enumeration of values are:" );
while (enu.hasMoreElements()) {
System.out.println(enu.nextElement());
}
}
}
|
Output:
The Stack is: [Geeks, 4, Geeks, Welcomes, You]
The enumeration of values are:
Geeks
4
Geeks
Welcomes
You
Program 2 :
import java.util.*;
public class Stack_Demo {
public static void main(String[] args)
{
Stack<Integer> stack = new Stack<Integer>();
stack.add( 10 );
stack.add( 15 );
stack.add( 20 );
stack.add( 25 );
stack.add( 30 );
System.out.println( "The Stack is: " + stack);
Enumeration enu = stack.elements();
System.out.println( "The enumeration of values are:" );
while (enu.hasMoreElements()) {
System.out.println(enu.nextElement());
}
}
}
|
Output:
The Stack is: [10, 15, 20, 25, 30]
The enumeration of values are:
10
15
20
25
30
Share your thoughts in the comments
Please Login to comment...