2016-03-20 5 views
2

は、ここで私は、私はこれらの8エラーエラー(ほとんどがアンダーフロー入力を行うように見える)

java.lang.AssertionError: IsEmpty Error: isEmpty did not return true for empty stack after underflow. 

java.lang.AssertionError: Peek Error: Peeking at null value on top of stack did not return null. 

java.lang.AssertionError: Pop Error: Popping null value off stack did not return null. 

java.lang.AssertionError: Push Error: Pushed multiple string values, but failed to retrieve them in order (via pop). 

java.util.concurrent.TimeoutException (this test was labelled testReverseStringWithStack) 

java.lang.AssertionError: Size Error: Size did not return correct size after pushes after underflow. 

java.lang.AssertionError: Size Error: Size did not return 0 for empty stack after underflow. 

java.lang.AssertionError: Push Error: Pushed multiple int values, but failed to retrieve them in order (via pop). 
を取得する例すべてをテストするときに問題があるコード

import java.util.EmptyStackException; 

public class MyGenericStack<Item> implements MyGenericStackInterface<Item> { 
private java.util.ArrayList<Item> list = new java.util.ArrayList<Item>(); 

/* 
* Retrieve the item that was most recently added to the stack, 
* which is the item at the top of the stack. 
* The item is removed from the stack. 
*/ 
public Item pop() throws EmptyStackException{ 
    if (isEmpty()) throw new EmptyStackException(); 
    else{ 
     Item thing = null; 
     if(list.get(size()-1) == null){ 
      thing = null; 
     } 
     else{ 

      thing = list.get(size()-1); 
     } 
     return thing; 
    } 
} 

/* 
* Retrieve the item at the top of the stack. 
* Does not modify the stack. 
*/ 
public Item peek() throws EmptyStackException{ 
    if (isEmpty()) { 
     throw new EmptyStackException(); 
    } 
    else{return list.get(size()-1); 

    } 

}; 

/* 
* Add item to the top of the stack. 
*/ 
public void push(Item item){ 

    list.add(item); 
}; 

/* 
* Return true if the stack is empty 
*/ 
public boolean isEmpty(){ 
    return list.isEmpty(); 

} 

/* 
* Return the number of items on the stack 
*/ 
public int size(){ 
    return list.size(); 
}; 


} 

です

これを修正できる方法はありますか?どんな助けもありがとうございます。

+0

クラウドあなたは私たちにテストコードを教えてくれますか?実際のコードよりも、テストでの問題が多く見えます。 –

答えて

0

あなたisEmpty()方法は、基礎となるList<>分野の用語で定義されていますが、あなたのpop()方法は、実際には、リストから項目を削除しません - それは唯一のリストから項目を削除しませんリスト、上のget()呼び出します。したがって、push()/pop()へのいくつかの呼び出しの後に、isEmpty()size()が間違っていることがわかります。

0

私が見ることができる問題は、pop()が呼び出されると、リストから項目が削除されないということです。リストから返されたアイテムを削除します。これにより、アンダーフローに関連するエラーが発生します。

0

うわー、私はちょっと馬鹿だと感じます。問題は、popメソッドに "list.remove(size-1)"を追加するのを忘れてしまったことです。もし誰かがそれを指摘しなかったなら、それは気付かなかったでしょうか、ありがとう。

関連する問題