java 通用堆栈数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5293140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generic Stack Array
提问by Ionut Ungureanu
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
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 -> the implementation of the interface
StackArray.java -> 接口的实现
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!
谁能帮我?谢谢!
采纳答案by Mark Peters
The erasureof 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.
该删除的T[]
是Number[]
因为上界的T
IS Number
。因此你stack
真的被声明为一个Number[]
not an Object[]
。在您的构造函数中,您正试图分配一个Object[]
to stack
。创建一个Number[]
代替。
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)
).
你不应该实现类的原始版本。因此,您需要更新其他方法(例如push(T)
代替push(Object)
)。
回答by Scott
Change
改变
public void push(Object x)
to
到
public void push(Number x) //or T
回答by rich
Try changing line 5 of Main.java to be:
尝试将 Main.java 的第 5 行更改为:
StackArray<Integer> stiva = new StackArray<Integer>(10);
StackArray<Integer> stiva = new StackArray<Integer>(10);
(Or some other type that extends Number, as required where you've marked StackArray as ).
(或其他扩展 Number 的类型,根据需要将 StackArray 标记为 )。