我需要搜索对象集合并找到哪个对象包含与我读入的字符串匹配的“名称”变量。下面是每个学生对象的样子:
public Student(String name, String class)
{
this.name = name;
this.class = class;
}
我还在.equals()
员工类中写了这个方法来做我的对象比较。
public boolean equals(Student student)
{
return this.name.equals(student.name);
}
在我的主要课程中,我将学生的姓名转换为一个Student
对象,并使用该.equals()
方法将其与其他每个学生进行比较。
public static void loadStudentProjects(ArrayList students)
Student currentStudent;
String studentName = "name";
while (count < students.size())
{
currentStudent = Students.create(studentName);
//The Student object is initialized as (name, null)
System.out.println(currentStudent.equals(students.get(count)));
count++;
即使我知道第一次比较应该显示名称匹配,此代码也会为每次比较返回 false。有人告诉我,我需要将要比较的字符串名称转换为对象并使用.equals()
方法,但我找不到让它工作的方法。