开发者

Array List Search

开发者 https://www.devze.com 2023-03-10 05:44 出处:网络
I\'m doing some past exam papers in preparation for a forthcoming exam, and have come across this question, and I\'m no开发者_运维技巧t sure how to solve it, I\'ve written a for-each loop with an if s

I'm doing some past exam papers in preparation for a forthcoming exam, and have come across this question, and I'm no开发者_运维技巧t sure how to solve it, I've written a for-each loop with an if statement but I'm not sure what the header would be and what to return. Any help would be great. Thanks.

The question:

An ArrayList named classList is used to store Student objects.

Write code for a method that determines whether or not data for a student with given forename and surname is present in classList.


Something like:

boolean lala( Student given ) {
  for( Student s : classList ) {
    if( s.getForename().equals( given.getForename() ) &&
        s.getSurname().equals( given.getSurname() ) ) {
      return true;
    }
  }

  return false;
}


static boolean isPresentInList(List<Student> students, String forename, String surname)
{
     // loop thru students and check forename and surname and if there is a match, 
     // return true
     // outside of loop return false (no match).

     ...
}


Something like this?

private static ArrayList<Student> findStudents(ArrayList<Student> students, Student student) {
    ArrayList<Student> result = new ArrayList<Student>();
    for (Student obj: students) {
        if (obj.getFirstname().equals(student.getFirstname()) &&
            obj.getLastname().equals(student.getLastname())) {
            result.add(obj);
        }
    }
    return result;
}

class Student {
    private String mFirstname;
    private String mLastname;

    public Student() {

    }

    public Student(String fistName, String lastName) {
        mFirstname = fistName;
        mLastname = lastName;
    }

    public String getFirstname() {
        return mFirstname;
    }

    public void setFirstname(String mFirstname) {
        this.mFirstname = mFirstname;
    }

    public String getLastname() {
        return mLastname;
    }

    public void setLastname(String mLastName) {
        this.mLastname = mLastName;
    }
}

public class Main {
    public static void main(String[] args) {
        ArrayList<Student> studentList = new ArrayList<Student>();
        studentList.add(new Student("eder", "quiñones"));
        studentList.add(new Student("eder", "orteag"));
        studentList.add(new Student("lucia", "germes"));
        studentList.add(new Student("chespirito", "gomez"));

        System.out.println(findAllStudentsByName(studentList, "eder"));
        System.out.println(findAllStudentsByName(studentList, "lucia"));
        System.out.println(findAllStudentsByLastName(studentList, "germes"));
        System.out.println(findStudents(studentList, new Student("eder", "quiñones")));
    }

    private static ArrayList<Student> findAllStudentsByName(ArrayList<Student> students, String firstName) {
        ArrayList<Student> result = new ArrayList<Student>();
        for (Student obj: students) {
            if (obj.getFirstname().equals(firstName)) {
                result.add(obj);
            }
        }
        return result;
    }

    private static ArrayList<Student> findAllStudentsByLastName(ArrayList<Student> students, String lastName) {
        ArrayList<Student> result = new ArrayList<Student>();
        for (Student obj: students) {
            if (obj.getLastname().equals(lastName)) {
                result.add(obj);
            }
        }
        return result;        
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消