通用堆栈数组

时间:2011-03-13 23:10:05

标签: java arrays generics stack

我必须实现一个通用堆栈,但是当我尝试构建项目时,我遇到了一个我无法弄清楚的错误。这是代码:

Stack.java - >接口

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 - &gt;接口的实现

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];
    }

}

主要课程:

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()+" ");
    }

}

当我尝试构建项目时,收到以下错误:

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

任何人都可以帮助我吗?谢谢!

3 个答案:

答案 0 :(得分:3)

T[]的{​​{3}}为Number[],因为T的上限为Number。因此,您的stack确实被声明为Number[]而不是Object[]。在您的构造函数中,您尝试将Object[]分配给stack。改为创建Number[]

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

顺便说一下,你可能想要

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

您不应该实现该类的原始版本。因此,您需要更新其他方法(例如push(T)而不是push(Object))。

答案 1 :(得分:0)

尝试将Main.java的第5行更改为:

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

(或者其他一些扩展Number的类型,根据需要将StackArray标记为)。

答案 2 :(得分:0)

更改

public void push(Object x)

public void push(Number x) //or T