2011-11-16 39 views
1

有一個LinkedQueueClass有3個要素:如何用字符串值搜索LinkedQueueClass

"ABC" 
"XYZ" 
"123" 

的元素中的LinkedListQueue是定製類類型StringElement其中有一個數據成員,它擁有一個字符串(字數據會員)。

StringElementhttp://pastebin.com/zh0t2X5K

當我嘗試使用search()功能來搜索,實際上在隊列中的boolean值返回是false存在的元素。意味着它無法找到元素。

Mainhttp://pastebin.com/vGegkcLZ

我在哪裏出了錯?

EDIT(代碼從Pastebin.org鏈接如上所述。)

主要:

public class StringElement extends DataElement { 

    protected String word; 

    public StringElement(String str) 
    { 
     word = str; 
    } 

    public StringElement(StringElement otherElement) 
    { 
     word = otherElement.word; 
    } 

    @Override 
    public boolean equals(DataElement otherElement) { 
     StringElement temp = (StringElement) otherElement; 
     return (word == temp.word); 
    } 

    @Override 
    public int compareTo(DataElement otherElement) { 
     throw new UnsupportedOperationException("Not supported yet."); 
    } 

    @Override 
    public void makeCopy(DataElement otherElement) { 
     StringElement temp = (StringElement) otherElement; 
     word = temp.word; 
    } 

    @Override 
    public DataElement getCopy() { 
     StringElement temp = new StringElement(word); 
     return temp; 
    } 

    @Override 
    public String toString() 
    { 
     return String.valueOf(word); 
    } 

} 

StringElement:

import java.util.Scanner; 

public class Run { 
    public static void main(String args[]){ 
     LinkedQueueClass strQueue = new LinkedQueueClass();  

     strQueue.addQueue(new StringElement("ABC")); 
     strQueue.addQueue(new StringElement("XYZ")); 
     strQueue.addQueue(new StringElement("123")); 

     System.out.println(); 

     //ask user for a keyword to search for 
     System.out.print("Search keyword: "); 
     Scanner scan = new Scanner(System.in); 
     String userInput; 
     userInput = scan.next(); 

     //place the entered keyword into a StringElement and use it 
     //to search for the element with mathing keyword 
     StringElement keyword = new StringElement(userInput); 
     System.out.println(keyword.toString());//debugging: to confirm userInput value got stored in keyword object 
     System.out.println(strQueue.search(keyword));//search returns false 


    } 
} 

搜索()從UnorderedLinkedList:

public boolean search(DataElement searchItem) 
    { 
     Node current; //pointer to traverse the list 
      boolean found; 

      current = first; //set current pointing to the first 
                //node in the list 

       found = false; //set found to false 

       while(current != null && !found)    //search the list 
         if(current.info.equals(searchItem)) //item is found 
         found = true; 
         else 
           current = current.link; //make current point to 
                     //the next node 
       return found; 
    } 
+0

您構建的關鍵字對象是從搜索對象不同。您正嘗試搜索不同的對象,但內容相同。 – r0ast3d

回答

0

解決方案

重寫DataElement的抽象equals()方法,像這樣:

public boolean equals(DataElement otherElement) { 
     StringElement temp = (StringElement) otherElement; 
     return (str.compareToIgnoreCase(temp.str) == 0); 
    } 
2

您的.equals實現使用==,它將檢查值相等性,這正是您要查找的內容。

看來您在搜索方法中使用的.equals不是您所期望的。這就是爲什麼你不應該混合使用==和.equals功能。

以一致的引用平等方式實現您的.equals更安全,然後使用==在您的搜索方法中執行值相等性檢查。

特別是,this是您使用.equals方法體驗到的怪癖。你從對象覆蓋的東西,而不是DataElement

+0

.equals來自UnorderedLinkedList類,它在search()裏面的意思是? – Bob

+0

您的代碼存在的問題是您實際上並沒有在您的StringElement類中重寫.equals。概念上的問題是,你正在使用.equals,你需要== –

+0

首先嚐試用'public boolean equals(object otherElement)替換'public boolean equals(DataElement otherElement)'' –