你如何找到某些参数?

时间:2014-03-05 02:54:59

标签: java arraylist

例如我有:

ArrayList<Student> stu = new ArrayList<Student>();
stu.add(new HighSchoolStudent("Jem", "Finch", 11, 3.4));
stu.add(new Student("Scout", "Finch", 4));
stu.add(new HighSchoolStudent("Boo", "Radley", 12, 1.7));
stu.add(new HighSchoolStudent("Atticus", "Finch", 12, 4.8));
stu.add(new Student("Patrick", "Henry", 9));
stu.add(new Student("Patrick", "Henry", 11));

ArrayList<Teacher> tea = new ArrayList<Teacher>();
tea.add(new Teacher("Betsy", "Ross", "Home Ec"));
tea.add(new Teacher("Ada", "Lovelace", "Mathematics"));
tea.add(new Teacher("Grace", "Hopper", "Computer Science"));
tea.add(new Teacher("Marie", "Curie", "Chemistry"));
tea.add(new Teacher("Dolly", "Madison", "Government"));
tea.add(new Teacher("Maya", "Angelou", "English Composition"));

School sch = new School(stu, tea);
System.out.println("\n\njust seniors: \n" + sch.getGradeLevel(12));

并且HighSchoolStudent和Student都有参数(LastName,FirstName,GradeLevel,gpa)。我如何在另一个类中创建一个方法(getGradeLevel),当找到GradeLevel为12时,它只打印arrayList的那部分?所以我希望输出为:

Radley, Boo
   Grade Level: 12
   ID #: 5
   GPA: 1.7
Finch, Atticus
   Grade Level: 12
   ID #: 6
   GPA: 4.8

我的学校班级代码是

class School
{
 public ArrayList <Student> student = new ArrayList<Student>();
 public ArrayList <Teacher> teacher = new ArrayList<Teacher>();
 public String listOfNames;
 private String gradeList;
public School()
{
}
public School(ArrayList <Student> stu, ArrayList <Teacher> tec)
{
student = stu;
teacher = tec;
}

public String getGradeLevel(int grade)
{
//Need help with this part
}




public String toString()
{//Makes a string with last, first name and subject
 listOfNames+=("\nFaculty:\n");
for (int i=0;i<teacher.size();i++)
 {
  listOfNames +=" "+teacher.get(i);
}

listOfNames+="\nStudents:\n";
for (int i=0;i<student.size();i++)
{
  listOfNames +=student.get(i)+" ";
}
return listOfNames;
}//End toString
}

3 个答案:

答案 0 :(得分:1)

for(Student s:stu){
  if(s.getGradeLevel()==12){
    System.out.println(s);
  }
}

此答案假定您已在getGradeLevel()课程中声明了一个方法Student,该方法会返回学生的成绩水平,并且您已正确覆盖Student.toString()

答案 1 :(得分:0)

使用getter方法......

通过构造函数为对象赋值时,只需使用这些值设置实例字段的值即可。然后,当你在帖子中提到使用getSomeValue()方法时,getter方法会找到这些字段的值;这是非常好的封装。封装是一种很好的编码实践。您可以通过调用指向所述对象的对象引用上的方法来访问对象的特定字段。

下面是一些示例代码来说明这一点。您可以根据具体目的轻松修改它。

示例代码:

package SchoolRulez;

import java.util.ArrayList;

public class Student { 

  private String school;
  private String name;
  private double GPA;
  private int grade;

  ArrayList<Student> students = new ArrayList<Student>();

 Student(String school, String name, double GPA, int grade) { 
     this.school = school;
     this.name = name;
     this.GPA = GPA;
     this.grade = grade;

 }

 public void addStudent(Student s) { 

         students.add(s); 
         return;
 }

 public double getGPA() { 

    return this.GPA;
 }

 public int getGrade() { 
   return this.grade;

 }

 public String getName(){ 
  return this.name;

 }


 // the rest of the 'getter' methods can follow the pattern of getGPA()  and getGrade() above :D

//find a specific grade level
 public void getGradeLevel(int grade) { 

     for(Student s: students) { 

        if(s.getGrade() == grade) { 
              System.out.println("this student " + s.getName() + " is in " + s.getGrade() + " grade");

        } // end if
      }// end enhanced for loop


 }// end getGradeLevel() method




}//end class

答案 2 :(得分:0)

您应该考虑使用HashMap

HashMap<Integer, ArrayList<Student>> = stu  new HashMap<Integer, ArrayList<Student>>();

ArrayList<Student> studentsGrade4 = new ArrayList<Student>();
studentsGrade4.add(new Student("Scout", "Finch", 4));
stu.put(new Integer(4), studentsGrade4);
ArrayList<HighSchoolStudent> studentsGrade11 = new ArrayList<HighSchoolStudent>();
studentsGrade12.add(new Student("Tom", "Skreech", 12));
studentsGrade12.add(new Student("Patrick", "Henry", 12))
stu.put(new Integer(12), studentsGrade11);

ArrayList<Student> highschoolSeniors = stu.get(12);
for (Student s : highSchoolSeniors) {
  System.out.println(s.toString());
}

这很有用,因为它会根据每个年级将学生分开。通过迭代学生列表填充HashMap会更容易。如果您有大量学生并且需要非常快速地检索成绩水平,那就太棒了。