2011-10-07 59 views
0

嘿,我寫了這個deleteNode()方法,如果我使用數字(int),但不會當我嘗試傳遞一個字符串時工作。我打印出一個字符串[]名單的列表,我試圖從列表中刪除某個名字。當我輸入名稱時,它會打印「找不到節點」。就像我說的,如果我打印出一個數字列表,它的效果很好,但如果我改變並打印一個字符串,它不會。任何幫助表示讚賞。如何從鏈表中刪除一個節點

public class BigNode { 


    public String dataitems; 
    public BigNode next; 
    BigNode front ; 

    public void initList(){ 
     front = null; 
    } 

    public BigNode makeNode(String number){ 
     BigNode newNode; 
     newNode = new BigNode(); 
     newNode.dataitems = number; 
     newNode.next = null; 
     return newNode; 
    } 

    public boolean isListEmpty(BigNode front){ 
     boolean balance; 
     if (front == null){ 
      balance = true; 
     } 
     else { 
      balance = false; 
     } 
     return balance; 

    } 

    public BigNode findTail(BigNode front) { 
     BigNode current; 
     current = front; 
     while(current.next != null){ 
      //System.out.print(current.dataitems); 
      current = current.next; 

     } //System.out.println(current.dataitems); 
     return current; 
    } 

    public void addNode(BigNode front ,String number){ 
     BigNode tail; 
     if(isListEmpty(front)){ 
      this.front = makeNode(number); 
     } 
     else { 
      tail = findTail(front); 
      tail.next = makeNode(number); 
     } 
    } 
    public void deleteNode(BigNode front, String value) { 
     BigNode curr, previous = null; boolean found; 

      if (!isListEmpty(front)){ 
       curr = front; 
       found = false; 

       while ((curr.next != null) && (!found)) { 
        if(curr.dataitems.equals(value)) { 
         found = true; 
        } 
        else { 
         previous = curr; 
         curr = curr.next; 
        } 
       } 
       if (!found) { 
        if(curr.dataitems.equals(value)) { 
         found = true; 
        } 
       } 
       if (found) { 
        if (curr.dataitems.equals(front.dataitems)){ // front.dataitems may be wrong .dataitems 
         front = curr.next; 
        } else { 

         previous.next = curr.next; 
        } 
       } else { 
        System.out.println("Node not found!"); 
        //curr.next = null; // Not sure If this is needed 
       } 
     } 
      showList(front); 
    } 




    public void printNodes(String[] len){ 


     int j; 
     for (j = 0; j < len.length; j++){ 

      addNode(front, len[j]); 
     } showList(front); 
    } 

    public void showList(BigNode front){ 
     BigNode current; 
     current = front; 
     while (current.next != null){ 
      System.out.print(current.dataitems + ", "); 
      current = current.next; 
     } 
     System.out.println(current.dataitems); 
    } 
    public static void main(String[] args) { 
        String[] names = {"Billy Joe", "Sally Mae", "Joe Blow", "Tasha Blue"}; 

     BigNode x = new BigNode(); 
        x.printNodes(names); 
        Scanner in = new Scanner(System.in); 
        String delete = in.next(); 
        x.deleteNode(x.front, delete); 
      } 

的String []名稱= {1,名稱2,名稱3,名稱4}

- 首先它打印列表,然後要求刪除什麼名字。

+0

確定繼承人的大部分代碼 – TMan

回答

3

編輯:好的,我發現了什麼不對的,你已經發布的示例代碼。

你打電話Scanner.next(),它讀取單個。所有的節點值都是兩字。所以如果我輸入「Sally Mae」,它實際上是只是尋找「Sally」。

這與BigNode中的大部分代碼無關(儘管這當然可以變得更加優雅)。基本上這樣的:

String delete = in.next(); 

應該

String delete = in.nextLine(); 

現在我會強烈建議你只是更改代碼,而是想想你可能已經確診本作的方式你自己:

  • 將日誌記錄添加到您的代碼中,以顯示您正在查找的值以及您測試時的每個值
  • 使用調試器來遍歷代碼,觀察變量
  • 使用單元測試來測試代碼 - 這些代碼不會向您顯示問題(因爲它不在代碼中,您通常會編寫測試)但他們會給你更大的信心,在測試代碼中問題不是

如果您嘗試這些方法中的一些或preferrably 所有,你將有更多的來自這個問題不僅僅是如何使用Scanner學到了很多東西......


在不同的地方,您使用==運算符比較字符串引用。如果您傳入對列表中存在的實際字符串對象之一的引用,那麼只會找到您的節點 - 而不是對等於字符串對象的引用。

你想要的東西,如:

if (curr.dataitems.equals(value)) 

(但仔細null檢查)。

+0

這沒有工作我仍然沒有找到節點 – TMan

+0

@TMan:那麼你需要發佈一個簡短的*完整*程序。例如,您仍然沒有向我們展示BigNode的代碼。 –

+0

好吧,我發佈了必要的代碼。你認爲這可能是我傳遞我想刪除的名字的方式? – TMan

0

您應該總是使用equals()來比較對象值,而不是==。例如,在你的這行代碼:

curr.dataitems == value 

應該被寫爲:

curr.dataitems.equals(value) 
2

您應該使用String.equals比較而不是==比較。

if(curr.dataitems == value) { 

應該是:

if(curr.dataitems.equals(value) { 
1

你做的比較與==。對於int這比較的值,但String只有參考。所以你想要刪除一個具有相同值的字符串,但是在不同的對象中這會失敗。

改爲使用String.equals()