Générique De La Pile De Tableau

- Je mettre en place un générique de la pile, mais quand j'essaie de compiler le projet, j'ai une erreur que je ne peux pas comprendre. Voici le 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 -> la mise en œuvre de l'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];
}
}

Et la classe principale:

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

Quand j'essaie de compiler le projet, j'ai l'erreur suivante:

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

Quelqu'un peut m'aider? Merci!

Aucune raison particulière pour laquelle vous ne souhaitez pas utiliser [Pile][1] ou d'une mise en œuvre de [Deque][2]? [1]: download.oracle.com/javase/6/docs/api/java/util/Stack.html [2]: download.oracle.com/javase/6/docs/api/java/util/Deque.html
Une autre chose; il ne semble pas être une bonne raison pour max être null; en fait, votre code sera probablement pas assez catastrophique si les gens utilisent les deux instances de votre pile à la fois.

OriginalL'auteur Ionut Ungureanu | 2011-03-13