【问题标题】:Linear search of a linked list [closed]链表的线性搜索[关闭]
【发布时间】:2015-08-03 20:12:06
【问题描述】:

我正在尝试对linked list 进行线性搜索。一个搜索是int,另一个是String。我究竟做错了什么? **根据建议更新了代码。

主内

    public static LinkedList<Contributor> contributorList = new LinkedList<>();

        String searchKey = "Jones";
        int intSearchKey = 45;

        System.out.println("Search key " + searchKey + " is found? " + sequentialSearch(contributorList, searchKey));

        System.out.println("Search key " + intSearchKey + " is found? " + sequentialSearch(contributorList, intSearchKey));



Called methods    



    public static <Contributor> boolean sequentialSearch(Iterable<Contributor> contributorList, int intSearchKey) {
    Iterator<Contributor> iter = contributorList.iterator();
    while (iter.hasNext()) {
        if (iter.next().equals(intSearchKey)) {
            return true;
        }
        iter = (Iterator<Contributor>) iter.next();
    }
    return false;
}

public static <Contributor> boolean sequentialSearch(Iterable<Contributor> contributorList, String searchKey) {
    Iterator<Contributor> iter = contributorList.iterator();
    while (iter.hasNext()) {
        if (iter.next().equals(searchKey)) {
            return true;
        }
        iter = (Iterator<Contributor>) iter.next();
    }
    return false;
}

【问题讨论】:

  • 我在你的代码中没有看到链表。
  • 你实现的东西最好用search 而不是contains 来描述——你的方法不会返回T 项目。
  • 为什么不直接使用contributorList.contains(key)
  • 一个Iterable 不是一个链表(而是一个LinkedList 一个Iterable)。因此,您的问题实际上是关于检查可迭代对象中的包含情况,因为 sequentialSearch 方法与链表结构无关。此外,使用名称 collection 调用 Iterable 会变得相当混乱,即使是中等大小的代码。
  • 真正的问题是什么?您基本上只是告诉我们它不起作用,而没有告诉我们如何它不起作用。您的代码也有点令人困惑,因为您的代码似乎存在于方法块之外,但可能存在于方法块内部(如sequentialSearch (contributorList, "Jones");)。如果没有看到您正在使用的实际代码,很难提供帮助。

标签: java search linked-list linear-search


【解决方案1】:

这一行将 Contributor 对象与 String 进行比较。

if (iter.next().equals(searchKey)) {

没有看到 Contributor 对象,我猜你想要这样的东西

if (iter.next().getKey().equals(searchKey)) {

另外,这行没有意义:

 iter = (Iterator<Contributor>) iter.next();

iter.next() 返回元素类型,而不是迭代器

【讨论】:

    【解决方案2】:

    看看你这里的代码:

    Iterator<Contributor> iter = contributorList.iterator();
    while (iter.hasNext()) {
        if (iter.next().equals(intSearchKey)) {
            return true;
        }
        iter = (Iterator<Contributor>) iter.next();
    }
    

    请注意,在第一次调用 .next() 时,您希望它返回一个 Contributor 对象。在第二种情况下,您期望它返回可以转换为 Iterator&lt;Contributor&gt; 的内容。

    我认为您对迭代器在 Java 中的工作方式存在根本性的误解,这就是代码不起作用的原因。迭代器上的.next() 方法自动将迭代器向前推进——它修改了接收器——并返回正在迭代的集合中的下一个值。这意味着在调用.next() 时不应为iter 分配新值,因为您将拥有不兼容的类型。相反,您可能应该像这样构建代码:

    Iterator<Contributor> iter = contributorList.iterator();
    while (iter.hasNext()) {
        Contributor currElem = iter.next();
        if (currElem.equals(intSearchKey)) {
            return true;
        }
    }
    

    请注意,您在循环中只调用一次.next() 来获取值,然后在当前循环迭代中使用该值。您永远不会重新分配iter,因为iter 会在您反复调用.next() 时自动遍历集合。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-26
      • 1970-01-01
      • 2015-02-21
      • 1970-01-01
      • 1970-01-01
      • 2017-07-14
      • 1970-01-01
      • 2011-05-18
      相关资源
      最近更新 更多