数组列表搜索

时间:2011-06-02 20:59:32

标签: java arraylist

我正在做一些过去的考试试卷,为即将到来的考试做准备,并且遇到了这个问题,我不知道如何解决它,我已经用if语句写了一个for-each循环但是我不确定标题是什么以及返回什么。任何帮助都会很棒。感谢。

问题:

  

使用名为classList的ArrayList   存储学生对象。

     

编写方法的代码   确定数据是否为   有学名的学生   surname存在于classList中。

3 个答案:

答案 0 :(得分:6)

类似的东西:

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

  return false;
}

答案 1 :(得分:2)

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).

     ...
}

答案 2 :(得分:1)

这样的东西?

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;        
    }
}