簡體   English   中英

您如何找到某些參數?

[英]How do you find certain parameters?

例如,我有:

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
}
for(Student s:stu){
  if(s.getGradeLevel()==12){
    System.out.println(s);
  }
}

該答案假定您已在Student類中聲明了一種getGradeLevel()方法,該方法返回學生的年級,並且已正確覆蓋了Student.toString()

使用吸氣劑方法...

通過構造函數將值分配給對象時,只需使用這些值設置實例字段的值即可。 然后,如您在帖子中提到的那樣,使用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

您應該考慮使用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甚至更加容易。 當您有大量學生並且需要非常快地檢索年級水平時,這非常好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM