このエラーが発生しました。エラーが発生したクラスは次のとおりです。エラー:互換性のない型:java.lang.ObjectをEに変換できません
/** 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;
}
}
は、ここでそれが必要なの念にStackクラスです:
/** 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);
}
エラーがプッシュ(オブジェクトのターゲット)メソッドでは、ArrayStackクラスのラインdata[size] = target;
に関してです。
少数の考えは:1) 'ArrayStackは単に' Stack'を実装するのではなく、 'スタックを実装しています。 2)次に、 'Object'という言葉をすべて取り除くことができます。また、 'new E [data.length * 2]'が動作するはずです。 –
jensgram