按多个对象属性搜索ArrayList

时间:2015-02-28 18:11:40

标签: java arraylist

我的班级Person包含名字和姓氏,班级Student包含平均成绩和最终成绩,继承了人。

public class Student extends Person
{
    private double HomeworkAve;
    private double QuizAve;
    private double ProjectAve;
    private double TestAve;
    private double FinalGrade;
    private String LetterGrade;

    public Student(){}

    public Student(String FName, String LName, double HomeworkAve, double QuizAve, double ProjectAve, double TestAve) {

        super(FName, LName);

        this.HomeworkAve = HomeworkAve;
        this.QuizAve = QuizAve;
        this.ProjectAve = ProjectAve;
        this.TestAve = TestAve;
    }



    public void setHomeworkAve(double HomeworkAve) {
        this.HomeworkAve = HomeworkAve;
    }

    public void setQuizAve(double QuizAve) {
        this.QuizAve = QuizAve;
    }

    public void setProjectAve(double ProjectAve) {
        this.ProjectAve = ProjectAve;
    }

    public void setTestAve(double TestAve) {
        this.TestAve = TestAve;
    }

我希望能够按名字搜索学生,所以在我的主要课程中,我使用了这个:

for (int i = 0; i < aloStudent.size(); i++) {
    String xsFirstName = aloStudent.get(i).getFName();

    if (xsFirstName.equals(xsFName) == true) 
    {
        System.out.println("found");
    }
}

在调试时,我注意到xsFirstName具有正确的值,但是当它与xsFName(用户的输入)进行比较时,它会跳过println。我做错了什么?

1 个答案:

答案 0 :(得分:0)

您应该考虑使用较短的版本

if (aloStudent.stream().anyMatch(s -> s.getFName().equals(xsFName))) {
    System.println("found");
}