簡體   English   中英

錯誤:類型不兼容:java.lang.Object 無法轉換為 E

[英]Error: incompatible types: java.lang.Object cannot be converted to 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);  

 }  

錯誤是關於行data[size] = target; 在 ArrayStack 類中的 push(Object target) 方法中。

data[size] = target;

1)這里的data指的是E arraytarget指的是Object
泛型帶來類型安全。 所以你不能從 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() {  

目標和數據不是同一類型。 您可以在需要類型的任何地方使用E 這使您的代碼更加類型安全。

將推送方法更改為:

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

由於您的數據數組是 E 類型:

private E[] data; 

你需要修改:

 public void push(Object target) { 

具有參數類型 E。

E 可以是運行時確定的字符串、整數等。 對象不等同於運行時類型。

試試這個:

/**
 * 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.
 */
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;
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM