我不太明白应该将哪些参数传递给 contains() 方法。我有自己的名为 Name 的类,它由 2 个字符串(firstName、secondName)组成。我创建了一个 Name 类型的 ArrayList 和 2 个同名的 Name 对象,例如("Joe, "Bloggs") 2x。那么我必须传递哪些参数来检查它是否有效。我已经正确地覆盖了 Name 类的 equals 方法。这是我的主程序:
import java.util.ArrayList;
public class EqualsTest {
public static void main(String[] args) {
ArrayList<Name> names = new ArrayList<Name>();
names.add(new Name("Joe", "Bloggs"));
names.add(new Name("John", "Smith")); //<--
names.add(new Name("Alan", "Wake")); // | the same name
names.add(new Name("Robert", "High"));// |
names.add(new Name("John", "Smith")); //---
names.contains(Name("Joe", "Bloggs"));
}
}
最佳答案
假设您已经在您的 Name
类中正确覆盖了 equals
,它应该是:
if (names.contains(new Name("Joe", "Bloggs"))) {
...
}
关于java - 我应该为 contains() 方法使用哪些参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28831581/