개체 컬렉션을 검색하고 내가 읽은 String과 일치하는 'name'변수가 포함 된 개체를 찾아야합니다. 아래는 각 Student 개체의 모습입니다..equals()를 사용하여 두 객체의 변수 비교
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()
메서드를 사용했지만 작동하도록하는 방법을 찾을 수 없습니다.
코드를 컴파일 확인하십시오 수 있습니다. 당신은 매개 변수 이름으로 "클래스"를 사용할 수 없습니다 – kritzikratzi
완벽한 함수'loadStudentProjects'를 붙여주세요. 또한 왜 'Employee'클래스의 'Student'에'.equals '가 있습니까? 오식? – Ankit