有两个class,一个用来存基本资料Studentpublic class Student
{
    // the student's full name
    private String name;
    // the student ID
    private String id;
    
    public Student(String fullName, String studentID)
    {
        name = fullName;
        id = studentID;
        
    }    /**
     * Return the full name of this student.
     */
    public String getName()
    {
        return name;
    }    /**
     * Set a new name for this student.
     */
    public void changeName(String replacementName)
    {
        name = replacementName;
    }    /**
     * Return the student ID of this student.
     */
    public String getStudentID()
    {
        return id;
    }
 
    
    /**
     * Print the student's name and ID number to the output terminal.
     */
    public void print()
    {
        System.out.println(name + " (" + id + ")");
    }
}
--------------------------------FGX-------------
这一个用来分配课室import java.util.*;public class LabClass
{
    
    private List<Student> students;
    private int capacity;
    
    /**
     * Create a LabClass with a maximum number of enrolments. All other details
     * are set to default values.
     */
    public LabClass(int maxNumberOfStudents)
    {
       
        students = new ArrayList<Student>();
        capacity = maxNumberOfStudents;
    }    /**
     * Add a student to this LabClass.
     */
    public void enrollStudent(Student newStudent)
    {
        if(students.size() == capacity) {
            System.out.println("The class is full, you cannot enrol.");
        }
        else {
            students.add(newStudent);
        }
    }
    
    /**
     * Return the number of students currently enrolled in this LabClass.
     */
    public int numberOfStudents()
    {
        return students.size();
    }
        
    public Student findStudent(String id)
    {
      
      
    }
    
    
    /**
     * Print out a class list with other LabClass details to the standard
     * terminal.
     */
    public void printList()
    {
       
        System.out.println("Class list:");
        for(Student student : students) {
            student.print();
        }
        System.out.println("Number of students: " + numberOfStudents());
    }
}现在问题是,要求完成查找学生id,我知道大约用if语句
public Student findStudent(String id)
    {
      
      
    }
但是我不懂arrayList应该怎么样调用外部class的方法,总是说代码错误
if(id == students.getStudentID());
       return students;请教大家到底应该怎么样写呢?谢谢各位啦。

解决方案 »

  1.   

    public Student findStudent(int id) {
    for (Student s : students) {
    if (s.getId() == id) {
    return s;
    }
    }
    return null;
    }
      

  2.   

    很简单啊,不过不清楚为啥你的id用String存的,可能不仅仅是数字
    你对ArrayList的操作还不熟悉,你需要学习下
    public Student findStudent(String id) {
    Student result=null;
    for(Student stu:students){
    if(stu.getStudentID().trim().equals(id.trim())){
    result=stu;
    break;
    }
    }
    return result;
    }
      

  3.   

    private List<Student> students;
    students = new ArrayList<Student>();
    你上面不是定义了嘛
    for(Student s : students){
        if(s.getStudentID.equals(id)){
            return s;
        }
    }
      

  4.   

    ArrayList<Student>中存储的就是Student向上转型为的Object对象,
    你直接for-each迭代调用该对象的方法嘛,
    而且泛型至1.5更新后自动转换的。
      

  5.   

    谢谢JQ_ii_QC,你的代码我懂了。谢谢
    ioe_gaoyong
    (风尘中国)
    我真的对arrayList不明白啊,我是初学者。的确这个class要用数字以外的东西来作为ID。