【问题标题】:Finding a value in an Array?在数组中查找值?
【发布时间】:2014-04-24 22:13:41
【问题描述】:

我正在尝试搜索数组中的值。结果不断回来,但什么都没有。我假设我的循环不正确,你能帮我解决我做错了什么吗?假设使用“searchFor”中保存的值并匹配。然后在 Printer.printPersonList 中显示该值。

    public static ArrayList<Person> findPerson(String searchFor) {
    ArrayList<Person> matches = new ArrayList<>();
    for (Person p : personList) {
        boolean isAMatch = false;

        if (p.getFirstName().equalsIgnoreCase(searchFor)) {
            isAMatch = true;

        } else if (p.getLastName().equalsIgnoreCase(searchFor)) {
            isAMatch = true;

        } else if (p.getSocialSecurityNumber().contains(searchFor)) {
            isAMatch = true;
            ;
        } else if (String.format("%d", p.getAge()).equals(searchFor))
            if (isAMatch) {

                matches.add(p);
            }

        Printer.printPersonList(matches);

    }
    return matches;

}

来自printer.java

public static void printPersonList(ArrayList<Person> personListToPrint) {

    System.out
            .printf("\nLast Name           First Name           Social Security Number  Age\n");
    System.out
            .printf("=================== ===================  ======================  ===\n");
    for (Person p : personListToPrint) {

        System.out.printf("%-20s%-21s%-24s%s\n", p.getLastName(),
                p.getLastName(), p.getSocialSecurityNumber(), p.getAge());

    }

【问题讨论】:

  • else if (String.format("%d", p.getAge()).equals(searchFor)) 没有有意义的正文。

标签: java loops arraylist


【解决方案1】:

你的算法的问题是你将isAMatch设置为真,如果这个人的名字等于你正在搜索的键,但没有将这个人添加到列表中。这是有缺陷的部分:

...code omitted...

} else if (String.format("%d", p.getAge()).equals(searchFor))
            if (isAMatch) {

                matches.add(p);
            }

        Printer.printPersonList(matches);

    }

...code omitted...

if(isAMatch) 代码仅在 String.format("%d", p.getAge()).equals(searchFor) 时执行 - 之后没有打开/关闭括号来指示代码块。

【讨论】:

    【解决方案2】:

    您似乎缺少一些代码。现在,只有当年龄匹配并且isAMatch 已经是true 时才会添加匹配项。你可能想要改变

    } else if (String.format("%d", p.getAge()).equals(searchFor))
        if (isAMatch) {
            matches.add(p);
        }
    

    } else if (String.format("%d", p.getAge()).equals(searchFor)) {
        isAMatch = true;
    }
    if (isAMatch) {
        matches.add(p);
    }
    

    【讨论】:

    • 仍然什么都不显示 :(
    • 此时您需要确定personList 是否包含任何项目,并通过检查每个Person 中的数据来确保您的搜索应该找到内容。
    【解决方案3】:

    你的代码应该是这样的:

    public static ArrayList<Person> findPerson(String searchFor) {
        ArrayList<Person> matches = new ArrayList<>();
        for (Person p : personList) {
            boolean isAMatch = false;
    
            if (p.getFirstName().equalsIgnoreCase(searchFor)) {
                isAMatch = true;
    
            } else if (p.getLastName().equalsIgnoreCase(searchFor)) {
                isAMatch = true;
    
            } else if (p.getSocialSecurityNumber().contains(searchFor)) {
                isAMatch = true;
    
            } else if (String.format("%d", p.getAge()).equals(searchFor)) {
               isAMatch = true;
                }
    
            if (isAMatch) {
    
                    matches.add(p);
                }
        }
        Printer.printPersonList(matches);
        return matches;
    
    }
    

    我修改了最后一个else if 语句,并将Printer 方法调用移出for 循环。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-07
      • 2014-02-09
      • 2012-09-04
      相关资源
      最近更新 更多