2013-10-10 18 views
-1

我收到了錯誤「類型的堆棧不帶參數的公共類ArrayStack實現棧」從這個代碼:不採取參數在Java中

public class ArrayStack<E> implements Stack<E> { 

private E[] data; 

private int size; 

public ArrayStack() { 
data = (E[])(new Object[1]); 
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]; 
} 

protected boolean isFull() { 
return size == data.length; 
} 

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

protected void stretch() { 
E[] newData = (E[])(new Object[data.length * 2]); 
for (int i = 0; i < data.length; i++) { 
    newData[i] = data[i]; 
} 
data = newData; 
} 
} 

「式的協議棧不帶參數的公共類ArrayStack工具堆棧「

堆棧類如下。

public interface Stack<E> { 

public boolean isEmpty(); 

public E peek(); 

public E pop(); 

public void push(E target); 

} 
+0

不知道什麼是錯誤的,但你的流行和PEEK在ArrayStack應返回E和你推法應該採取E. – Alowaniak

+0

你爲什麼要重新發布? – nhgrif

+0

通常,當人們重新發布**完全相同的問題時,通常意味着舊問題有0個答案。我不明白,你的其他問題有2個答案。 –

回答

0

你偷看()方法應該喜歡這個

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

你的push()方法應該如下

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

你的pop()方法應該如下

public E pop() throws EmptyStructureException { 
     if (isEmpty()) { 
      throw new EmptyStructureException(); 
     } 
     size--; 
     return (E)data[size]; 
    } 

現在你的界面看起來像belo w^

public interface Stack<E> { 

     public boolean isEmpty(); 

     public E peek() throws EmptyStructureException; 

     public E pop() throws EmptyStructureException; 

     public void push(E target); 

} 
相關問題