how to convert a Stack to a linear array using collections in java
Previous | Home | Next |
In this example we are going to convert a Stack to a linear array.
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 a user defined method name Stacktoarray(Stack stck, Integer[] array ), which takes two parameters that are, stack object and the array object. In this example we have created a Stack which contains the integer elements.
In the method Stacktoarray(Stack stck, Integer[] array), we have used the toArray(Anytypearray[] array) on the object of the Stack. The general syntax of the toArray(Anytypearray[] array) is given as,
Object[] toArray()
This method returns an array containing all of the elements of this stack.
In this example we have created a class named CollectionExample, in which we have created Stack, in which we have added some elements by using the push(Object obj) method on the object of the Stack. In our example we have created a stack that contains the integer values.
We have created an Integer [] array which is initialized to null. Then we have called the Stacktoarray(stck, array) method which private and static in nature. Inside this method we have defined the size of the array which is similar to the size of the stack which is shown by the following statements,array = new Integer[st.size()]
The we have invoked the toArray(Anytypearray[] array) method on the Stack object which is shown by the following statement in our example,
array = st.toArray(array);
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
Integer[] array=null; // creating an Integer array reference initialized with null value
Stacktoarray(stck, array); // calling the Stacktoarray defined below
}
catch(EmptyStackException e)
{
e.printStackTrace();
}
}
/* The following method converts a given stack to a linear array */
private static void Stacktoarray(Stack<Integer> st, Integer[] array)
{
array = new Integer[st.size()]; // Initializing the array to the same size as of the stack st
array = st.toArray(array); // conveting the stack st to array using toArray() method
System.out.println("The elements of the created array are as follows: ");
for(int i=0; i<array.length; i++)
{
System.out.print(array[i]+" "); // displaying the contents of the array thus created
}
System.out.println();
}
/* The above method converts a given stack to a linear array */
}
The output of the above example is:
After pushing elements on to the stack its status is: [1, 2, 3, 4, 5, 6, 7]
The searched element is: 5
The elements of the created array are as follows:
1 2 3 4 5 6 7
Previous | Home | Next |