如何在Java中设置搜索方法

huangapple go评论78阅读模式
英文:

how to set up search method in java

问题

I'm here to provide the translated code and text:

我是Java的新手我正在尝试创建一个名为`searchListBySurName`的方法该方法接受一个字符串参数并根据是否存在具有相应姓氏的姓名返回true或false

这是我目前的代码

public boolean searchListBySurName(String SurName)
{
    if (list.contains(SurName)) {
        return true;
    } else {
        return false;
    }
}

这是我用来测试该方法的测试数据:

public void testSearchListBySurName() {
  r.addName(new Name("Joe", "Bloggs"));
  r.addName(new Name("Fred", "Jones"));

  assertTrue("第一次搜索应该找到Jones,即返回true", r.searchListBySurName(new String("Jones")));

  r = new Register();
  r.addName(new Name("Joe", "Bloggs"));
  r.addName(new Name("Fred", "Wayne"));

  assertFalse("第二次搜索不应该找到Jones,即返回false", r.searchListBySurName(new String("Jones")));
}

但是,测试结果一直显示为失败。我犯了什么错误,该如何修复它?

英文:

I’m new to Java and I’m trying to make a method called searchListBySurName that accepts a String argument and returns true or false depending on whether a name exists with that respective last name.

Here is what I have so far:


public boolean searchListBySurName(String SurName)

{
    if (list.contains(SurName)) {

        return true;
    } else {
        return false;
    }
}

Here is the test data I’m using to test the method:

public void testSearchListBySurName() {
  r.addName(new Name("Joe", "Bloggs"));
  r.addName(new Name("Fred", "Jones"));

  assertTrue("First search should find Jones, i.e. return true", r.searchListBySurName(new String("Jones")));

  r = new Register();
  r.addName(new Name("Joe", "Bloggs"));
  r.addName(new Name("Fred", "Wayne"));

  assertFalse("Second search should not find Jones, i.e. return false", r.searchListBySurName(new String("Jones")));
}

But, the test keeps coming back as failed. What mistake am I making and how can I fix it?

答案1

得分: 5

你的列表是 Name,但你搜索的是 String,所以不会起作用。一种简单的方法是使用 for 循环:

public boolean searchListBySurname(String surname) {
    for (Name name : list)
        if (name.getSurname().equals(surname))
            return true;

    return false;
}

或者你可以使用Java 8的流:

public boolean searchListBySurname(String surname) {
    return list.stream().anyMatch(name -> name.getSurname().equals(surname));
}

P.S. 还要注意方法和参数名称。

英文:

Your list is of Name, but you search for String, so it won't work. The straight-forward way is with a for loop:

public boolean searchListBySurname(String surname) {
    for (Name name : list)
        if (name.getSurname().equals(surname))
            return true;

    return false;
}

Or you could use Java 8 streams:

public boolean searchListBySurname(String surname) {
    return list.stream().anyMatch(name -> name.getSurname().equals(surname));
}

P.S. Also pay attention to method and parameter names.

答案2

得分: 0

可以使用 list.contains() 方法,但是你需要重写 Name 类的 equals 方法。

使用 List.contains(Object object) 方法来判断 ArrayList 是否包含一个元素对象(在这种情况下,Name 的 SurName 值相同)应该重写 equals() 方法。如果没有重写 List<E> 元素对象中的 equals() 方法,它将导致 contains() 方法始终返回 false。

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    Name name = (Name) o;
    return Objects.equals(SurName, name.SurName);
}

searchListBySurName 方法中:

public boolean searchListBySurName(Name name) {
    return list.contains(name);
}

然后像这样使用它:

public void testSearchListBySurName() {
    r.addName(new Name("Joe", "Bloggs"));
    r.addName(new Name("Fred", "Jones"));

    assertTrue("第一次搜索应该找到 Jones,即返回 true", r.searchListBySurName(new Name("", "Jones")));

    r = new Register();
    r.addName(new Name("Joe", "Bloggs"));
    r.addName(new Name("Fred", "Wayne"));

    assertFalse("第二次搜索不应该找到 Jones,即返回 false", r.searchListBySurName(new Name("", "Jones")));
}
英文:

It is ok to use list.contains() method. But you need to override the equals method of Name Class.

Using the List.contains(Object object) method to determine whether the ArrayList contains an element object (for this case where the SurName value of the Name is the same) should override the equals() method. If the equals() method in the element Object of List&lt;E&gt; is not override, it will cause the contains() method to always return false.

@Override  
public boolean equals(Name o) {  
    if (this.SurName = o.SurName){
        return true;
    } else{
        return false;
    }
}  

In method searchListBySurName:

public boolean searchListBySurName(Name name)

{
    if (list.contains(name)) {
        return true;
    } else {
        return false;
    }
}

And Use it like this:

public void testSearchListBySurName() {
  r.addName(new Name(&quot;Joe&quot;, &quot;Bloggs&quot;));
  r.addName(new Name(&quot;Fred&quot;, &quot;Jones&quot;));

  assertTrue(&quot;First search should find Jones, i.e. return true&quot;, r.searchListBySurName(new Name(&quot;&quot;,&quot;Jones&quot;)));

  r = new Register();
  r.addName(new Name(&quot;Joe&quot;, &quot;Bloggs&quot;));
  r.addName(new Name(&quot;Fred&quot;, &quot;Wayne&quot;));

  assertFalse(&quot;Second search should not find Jones, i.e. return false&quot;, r.searchListBySurName(new Name(&quot;&quot;,&quot;Jones&quot;)));
}

huangapple
  • 本文由 发表于 2020年8月11日 06:56:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63349141.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定