4

I have to implement a generic stack, but when I try to build the project I have an error that I can't figure out. Here's the code:

Stack.java -> interface

package stack;

public interface Stack <T> {
    public boolean isEmpty();
    public boolean isFull();
    public void push(T x) throws StackFullException;
    public boolean offer(T x);
    public T pop() throws StackEmptyException;
    public T poll();
    public T peek() throws StackEmptyException;
    public T element();
}

StackArray.java -> the implementation of the interface

package stack;

public class StackArray <T extends Number> implements Stack {
    static int max;
    private int nr;
    private T[] stack;

    public StackArray(int size){
        nr=0;
        stack=(T[])(new Object[size]);
        max=size;
    }
    public boolean isEmpty() {
        if (nr<=0)
            return true;
        return false;
    }
    public boolean isFull() {
        if (nr==max-1)
            return true;
        return false;
    }
    public void push(Object x) throws StackFullException{
        if(isFull())
            throw new StackFullException();
        else
            stack[nr++]=(T)x;
    }
    public boolean offer(Object x) {
        if(isFull())
            return false;
        else
        {
            stack[nr++]=(T)x;
            return true;
        }
    }

    public T pop() throws StackEmptyException {
        T aux=(T)(new Object());
        if(isEmpty())
            throw new StackEmptyException();
        else
            {
                aux=stack[nr];
                stack[nr]=null;
                nr--;
                return aux;
            }
        }

    public T poll() {
        T aux=(T)(new Object());
        if(isEmpty())
            return null;
        else
        {
             aux=stack[nr];
             stack[nr]=null;
             nr--;
             return aux;
        }

    }

    public T peek() throws StackEmptyException {
        if(isEmpty())
            throw new StackEmptyException();
        else
            return stack[nr];
    }

    public T element() {
        if(isEmpty())
            return null;
        else
            return stack[nr];
    }

}

And the main class:

package stack;

public class Main {
    public static void main(String[] args) throws StackFullException, StackEmptyException {
        StackArray stiva=new StackArray(10);
        for(int i=1; i<10; i++)
            stiva.push(i);
        for(int i=1; i<10; i++)
            System.out.print(stiva.pop()+" ");
    }

}

When I try to build the project I receive the following error:

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Number;
        at stack.StackArray.<init>(StackArray.java:10)
        at stack.Main.main(Main.java:5)
Java Result: 1

Can anyone help me? Thanks!

bluefoot
  • 10,220
  • 11
  • 43
  • 56
Ionut Ungureanu
  • 380
  • 3
  • 9
  • 25
  • One other thing; there doesn't seem to be a good reason for `max` to be null; in fact, your code will probably fail pretty catastrophically if people use two instances of your stack at once. – Mark Peters Mar 14 '11 at 01:59
  • Any particular reason you don't want to use [Stack][1] or an implementation of [Deque][2]? [1]: http://download.oracle.com/javase/6/docs/api/java/util/Stack.html [2]: http://download.oracle.com/javase/6/docs/api/java/util/Deque.html – Peter Knego Mar 14 '11 at 01:09

3 Answers3

3

The erasure of T[] is Number[] because the upper bound of T is Number. Thus your stack is really declared as a Number[] not an Object[]. In your constructor you are trying to assign an Object[] to stack. Create a Number[] instead.

stack=(T[])(new Number[size]);

As an aside, you probably want

public class StackArray <T extends Number> implements Stack<T>

You shouldn't implement the raw version of the class. As a consequence you'll need to update other methods (e.g. push(T) instead of push(Object)).

Mark Peters
  • 80,126
  • 17
  • 159
  • 190
  • I've made those changes and now I have the following error: Exception in thread "main" java.lang.ClassCastException: java.lang.Object cannot be cast to java.lang.Number at stack.StackArray.pop(StackArray.java:40) at stack.Main.main(Main.java:9) Java Result: 1 Line 40 is T aux=(T)(new Object()); – Ionut Ungureanu Mar 13 '11 at 23:44
  • 1
    Why do you try to create a new object and then reassign the variable? Jus do `T aux;` - no need to initialize it (not mentioning that it's wrong). – Peter Knego Mar 14 '11 at 01:06
  • @Ionut: @Peter is right. There is absolutely no reason you need to create an object there. – Mark Peters Mar 14 '11 at 01:54
  • @Peter Knego: Thanks! That was the problem. I've changed to simple T aux and worked. Problem solved! – Ionut Ungureanu Mar 14 '11 at 11:22
  • @Ionut: any reason you don't want to use `Stack` or `ArrayDeque` provided by Java? – Peter Knego Mar 14 '11 at 12:24
  • @Peter: This is a homework. My teacher told me to implement the stack this way... educational purpose. – Ionut Ungureanu Mar 14 '11 at 13:39
0

Try changing line 5 of Main.java to be:

StackArray<Integer> stiva = new StackArray<Integer>(10);

(Or some other type that extends Number, as required where you've marked StackArray as ).

rich
  • 18,987
  • 11
  • 75
  • 101
0

Change

public void push(Object x)

to

public void push(Number x) //or T
brimborium
  • 9,362
  • 9
  • 48
  • 76
Scott
  • 16,711
  • 14
  • 75
  • 120