Völlig fest auf diesem Fehler. Hier ist die Klasse, von der der Fehler kommt.Fehler: inkompatible Typen: java.lang.Object kann nicht in E konvertiert werden
/** An array-based Stack. */
public class ArrayStack<E> implements Stack {
/** Array of items in this Stack. */
private E[] data;
/** Number of items currently in this Stack. */
private int size;
/** The Stack is initially empty. */
public ArrayStack() {
data = (E[])(new Object[1]); // This causes a compiler warning
size = 0;
}
public boolean isEmpty() {
return size == 0;
}
public Object pop() {
if (isEmpty()) {
throw new EmptyStructureException();
}
size--;
return data[size];
}
public Object peek() {
if (isEmpty()) {
throw new EmptyStructureException();
}
return data[size - 1];
}
/** Return true if data is full. */
protected boolean isFull() {
return size == data.length;
}
public void push(Object target) {
if (isFull()) {
stretch();
}
data[size] = target;
size++;
}
/** Double the length of data. */
protected void stretch() {
E[] newData = (E[])(new Object[data.length * 2]); // Warning
for (int i = 0; i < data.length; i++) {
newData[i] = data[i];
}
data = newData;
}
}
Hier ist die Stack-Klasse nur für den Fall es gebraucht wird:
/** A last-in, first-out stack. */
public interface Stack<E> {
/** Return true if this Stack is empty. */
public boolean isEmpty();
/**
* Return the top item on this Stack, but do not modify the Stack.
* @throws EmptyStructureException if this Stack is empty.
*/
public E peek();
/**
* Remove and return the top item on this Stack.
* @throws EmptyStructureException if this Stack is empty.
*/
public E pop();
/** Add target to the top of the Stack. */
public void push(E target);
}
Der Fehler ist in Bezug auf die Linie data[size] = target;
in der ArrayStack Klasse, in der Push (Object target) Methode.
Ein paar Gedanken: 1) 'ArrayStack implementiert Stapel ' statt einfach 'Stack' implementieren. 2) Dann können Sie alle "Objekt" Erwähnungen loswerden. Außerdem sollte 'new E [data.length * 2]' funktionieren. –
jensgram