オブジェクトのコレクションを検索して、読み込んだ文字列に一致する「name」変数が含まれているオブジェクトを見つける必要があります。各Studentオブジェクトは次のようになります。
public Student(String name, String class)
{
this.name = name;
this.class = class;
}
.equals()
また、オブジェクトの比較を行うために、employeeクラスで このメソッドを作成しました。
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()
、それを機能させる方法が見つかりません。