how to store the elements of a stack in an enumeration using collections in java
Previous | Home | Next |
In this example we are going to store the elements of a stack in an enumeration.
In this example we will create a stack. For creating a stack we have import the java.util package which contains the java.util.Stack class. In the following example we have created a class named CollectionExample in which we have created a Stack object and added some elements to it.
In this example we have created Stackelementusingenumeration(Stack stck, Enumeration enu ), which takes two parameters, one is Stack object and other is the Enumeration reference object. In this method we have used the elements() method on the stack object for obtaining the objects of the stack. Then we have showed the elements of the stack using the enumeration interface.
In this example we have created a Stack in which we have added the elements to the stack using the push(Object obj) on the stack object which is done as follows,stck.push(1);Enumeration<Integer> enu=null;
This statement places the integer number 1 at the top of the stack.
Then we have created an reference of the type Enumeration initialized to a null value. Then we have called the method we have created for storing the elements of the stack to an enumeration object.i.e. Stackelementusingenumeration(stck, enu). This is shown by the following statements in our example,
Stackelementsusingenumeration(stck, enu);
package r4r.co.in;
import java.util.*;
public class CollectionExample
{
/**
* @param args
*/
public static void main(String args[]) throws EmptyStackException
{
try
{
Stack<Integer> stck=new Stack<Integer>(); // creating a new stack
stck.push(1); // pushing element onto the stack
stck.push(2);
stck.push(3);
stck.push(4);
stck.push(5);
stck.push(6);
stck.push(7);
System.out.println("After pushing elements on to the stack its status is: "+stck); // displaying the contents of the stack
int srchele=stck.search(3); // here we are searching which element is at index 3
System.out.println("The searched element is: "+srchele); // displaying the searched element
Enumeration<Integer> enu=null; // creating an Enumeration reference object intialized to null
Stackelementsusingenumeration(stck, enu); // calling Stackelementsusingenumeration method defined below in the example
}
catch(EmptyStackException e)
{
e.printStackTrace();
}
}
/* The following method creates an Enumeration for holding the elements of the stack */
private static void Stackelementsusingenumeration(Stack<Integer> st, Enumeration enu)
{
enu = st.elements(); // stores the elements in the Enumeration object
System.out.println("The elements in the created Enumeration are: ");
while(enu.hasMoreElements()) // continue while enu has more elements remaining
{
System.out.println(enu.nextElement()); // displaying the elements in the enu
}
}
}
The output of this example is the following one :
After pushing elements on to the stack its status is: [1, 2, 3, 4, 5, 6, 7]
The searched element is: 5
The elements in the created Enumeration are:
1
2
3
4
5
6
7
Previous | Home | Next |