错误:不兼容的类型:java.lang.Object无法转换为E.

时间:2017-02-15 07:00:21

标签: java

完全坚持这个错误。这是错误来自的类。

 /** 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;相关,在push(对象目标)方法中。

4 个答案:

答案 0 :(得分:3)

data[size] = target;

1)此处data表示E arraytarget表示Object
泛型带来了类型安全性。所以你无法将对象转换为E。

2)public class ArrayStack<E> implements Stack {

不正确,因为您没有为要实现的接口提供参数化类型。
编写public class ArrayStack<E> implements Stack<E> {会更安全,并会强制您通过尊重Stack方法中使用的参数化类型来实现Stack方法。 例如:public void push(E target);

3)要符合ArrayStack中声明的参数化类型,您应该在方法中使用参数化类型而不是Object

所以你应该替换

 public void push(Object target) {  

通过

 public void push(E target) {  

你应该在声明操作声明类型Object而不是E的所有方法中做同样的事情。例如:

 public Object peek() 

 public Object pop() {  

答案 1 :(得分:1)

目标和数据的类型不同。您可以使用E您需要的类型。这使您的代码更安全。

将推送方法更改为:

public void push(E target) {  
 if (isFull()) {  
   stretch();  
 }  
 data[size] = target;  
 size++;  
}  

答案 2 :(得分:1)

由于您的数据数组是E类型:

private E[] data; 

您需要修改:

 public void push(Object target) { 

有参数类型E。

E可以是在运行时确定的String,Integer等。对象不等于运行时类型。

答案 3 :(得分:0)

Try this:


/**
* An array-based Stack.
*/
public class ArrayStack<E> implements Stack<E> {

/**
 * 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 E pop() {
    if (isEmpty()) {
        throw new RuntimeException();
    }
    size--;
    return data[size];
}

public E peek() {
    if (isEmpty()) {
        throw new RuntimeException();
    }
    return data[size - 1];
}

/**
 * Return true if data is full.
 *
 * @return
 */
protected boolean isFull() {
    return size == data.length;
}

public void push(E 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;
}
}