Java Question

Stack using stack in java

I have a problem with my homework. And I need all the help that you can give me.

i need to create a combination of a stack that can store any type of data and a Stack that works with elements of type Stack. I´m very confused.

The methods that I was supposed to implement were:


And this is what i have so far:

public class pilita {
Object vectorPila[];
int tope;
public pilita(int tam){
vectorPila=new Object[tam];
public void inicializarPila(){
public void push(Object dato){
public Object pop(){
Object fuera=vectorPila[tope];
return fuera;
public boolean pilaVacia(){
return tope==-1;
public boolean pilaLlena(){
return vectorPila.length-1==tope;
public Object cima(){
return vectorPila[tope];
public Object contar(){
return tope+1;
} }

All the methods are well implemented (Using my logic).

But how can i make an Stack using Stack data type using those methods?.
I would be very grateful if anyone could help me.

Also here is the original problem.

Stack of little Stacks: The elements of the Data Structure Little Stack are of any type of data. The elements of the Data Structure Stack are of Stack type.

Answer Source
// Java code for stack implementation

import java.util.*;

class Test
    // Pushing element on the top of the stack
    static void stack_push(Stack<Integer> stack)
        for(int i = 0; i < 5; i++)

    // Popping element from the top of the stack
    static void stack_pop(Stack<Integer> stack)
        System.out.println("Pop :");

        for(int i = 0; i < 5; i++)
            Integer y = (Integer) stack.pop();

    // Displaying element on the top of the stack
    static void stack_peek(Stack<Integer> stack)
        Integer element = (Integer) stack.peek();
        System.out.println("Element on stack top : " + element);

    // Searching element in the stack
    static void stack_search(Stack<Integer> stack, int element)
        Integer pos = (Integer);

        if(pos == -1)
            System.out.println("Element not found");
            System.out.println("Element is found at position " + pos);

    public static void main (String[] args)
        Stack<Integer> stack = new Stack<Integer>();

        stack_search(stack, 2);
        stack_search(stack, 6);

In Java, they are called Collections and the are analogous to the standard template library(STL) of C++.Now, what you can do is define a void type stack and typecast it for various type of elements.

In c, stack.h has the following stack structure

    typedef struct{
        void *elems;
        int elemSize;
        int allocLength;
        int logLength;
        void (*freefnc)(void *);
    } Stack;

void stackNew(Stack *s, int elemSize, void (*freefnc)(void *));
void stackDispose(Stack *s);
void stackPush(Stack *s, void *elemAddr);
void stackPop(Stack *s, void *target);

You can study more about them here:- (see collections in java) (see standard template library(STL))

for defining void type generics in java refer:-

What's the difference between Void and no parameter?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download