【问题标题】:How to search LinkedQueueClass with a string value如何使用字符串值搜索 LinkedQueueClass
【发布时间】:2011-11-16 20:21:20
【问题描述】:

LinkedQueueClass 有 3 个元素:

"ABC" 
"XYZ" 
"123"

LinkedListQueue 中的元素是自定义类类型StringElement,它有一个数据成员,它包含一个字符串(word 数据成员)。

StringElement: http://pastebin.com/zh0t2X5K

当我尝试使用search() 函数搜索队列中实际存在的元素时,它返回的boolean 值是false。表示找不到元素。

Main: http://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);
    }

}

字符串元素:

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;
    }

【问题讨论】:

  • 您构造的关键字对象与搜索对象不同。您正在尝试搜索不同的对象,尽管内容相同。

标签: java search queue linked-list


【解决方案1】:

您的 .equals 实现使用 == 来检查值是否相等,这正是您要寻找的。​​p>

您在搜索方法中使用的 .equals 似乎不是您所期望的。这就是为什么您不应该混合使用 == 和 .equals 功能的原因。

以一致的引用相等方式实现您的 .equals 会更安全,然后使用 == 在您的搜索方法中进行值相等检查。

特别是,this 是您在使用 .equals 方法时遇到的怪癖。您正在覆盖对象中的某些内容,而不是 DataElement

【讨论】:

  • .equals from UnorderedLinkedList 里面 search() 你的意思是?
  • 您的代码的问题在于您实际上并没有在 StringElement 类中覆盖 .equals 。概念上的问题是你在你想要的地方使用 .equals ==
  • 首先尝试将public boolean equals(DataElement otherElement)替换为public boolean equals(object otherElement)
【解决方案2】:

解决方案

像这样覆盖 DataElement 的抽象 equals() 方法:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    • 2020-06-10
    相关资源
    最近更新 更多