0

Possible Duplicate:
Java how to: Generic Array creation

import java.util.EmptyStackException;
import java.util.Vector;

public class Stack<E> extends Vector<E> {
    private E a[];
    private int top;

    public void Stack() {
        a = new E[100];
        top = -1;
    }

    public void Stack(int n) {
        a = new E[n];
        top = -1;
    }

    public E pop() {
        E obj;
        int len = size();
        if (top == -1)
            throw new EmptyStackException();
        else
            obj = a[top--];
        return obj;
    }

    public void push(E e) {
        if (e == null)
            throw new NullPointerException();
        else if (top == size() - 1)
            System.out.println("Stack full");
        else {
            a[++top] = e;
            System.out.println("pushed :" + e);
        }

    }

    public int size() {
        int i;
        for (i = 0; a[i] != null; i++)
            ;
        return i;
    }

}

This is my stack generics class in java. I am getting an error in array declaration inside the two constructor i.e Stack() and Stack(int n). The error is "generic array creation" in both cases. please help

Community
  • 1
  • 1
rick
  • 823
  • 6
  • 13
  • 27
  • 4
    Please see this question: http://stackoverflow.com/questions/529085/java-how-to-generic-array-creation – Mat Apr 11 '12 at 12:49
  • Your constructors are not really constructors, as constructors do not have a return type. Also, you are extending `Vector`, which means you don't need to have a local array. – Paaske Apr 11 '12 at 13:05

2 Answers2

1

You do not have constructors in your class these are methods. for construtors remove the void type. Constructors don't have return types. Also your class name starts with lower letter stack and constructors start with Stack . Rename your class to Stack and remove void types.

You need to do it like this

      public  Stack(Class<E> cls) 
 {
      a = (E[]) Array.newInstance( cls);

   top=-1;
 }

public  Stack(Class<E> cls,int n) 
  {
   a = (E[]) Array.newInstance( cls,n);
    top=-1;
  }

see Generic Array

Following is your working class with object creation in main method.

class Stack<E> extends Vector<E> {
    private E a[];
    private int top;

    public   Stack(Class<E> cls) 
    {


      a = (E[]) Array.newInstance( cls);

      top=-1;
    }

    public   Stack(Class<E> cls,int n) 
    {


      a = (E[]) Array.newInstance( cls,n);

      top=-1;
    }

    public E pop() {
        E obj;
        int len = size();
        if (top == -1)
            throw new EmptyStackException();
        else
            obj = a[top--];
        return obj;
    }

    public void push(E e) {
        if (e == null)
            throw new NullPointerException();
        else if (top == size() - 1)
            System.out.println("Stack full");
        else {
            a[++top] = e;
            System.out.println("pushed :" + e);
        }

    }

    public int size() {
        int i;
        for (i = 0; a[i] != null; i++)
            ;
        return i;
    }

  public static void main(String...strings ){
      Stack<Integer> st=new Stack<Integer>(Integer.class,n);
  }  

}
Community
  • 1
  • 1
Shehzad
  • 2,614
  • 13
  • 21
1

generic array is can not be created. so use Object array.


import java.io.*;
 import java.util.EmptyStackException;
 import java.util.Vector;
 public class Stack extends Vector 
 {
   private Object a[];
   private int top;
   public void Stack() 
  {
    a=new Object[100];
    top=-1;
  }
  public void Stack(int n) 
  {
    a=new Object[n];
    top=-1;
  }
  public E pop() 
  {
    E   obj;
    int len = size();
    if (top == -1)
        throw new EmptyStackException();
    else
       obj=(E) a[top--]; 
    return obj;
  }
  public void push(E e) 
  {
    if(e==null)
        throw new NullPointerException();
    else if(top==size()-1)
        System.out.println("Stack full");
    else
    {
       a[++top]=e;
       System.out.println("pushed :"+e);
    }


}
public int size() 
{
    int i;
    for(i=0;a[i]!=null;i++);
    return i;
}
}
Nagaraju Badaeni
  • 880
  • 8
  • 14