Java数组和引用

时间:2018-12-06 22:39:01

标签: java arrays

作为我学校课程的一部分,我们正在研究一些CodeHS Java。 我遇到了一个问题:

  

以前面的“学生和教室”示例为例,您应该填写方法getMostImprovedStudent和方法getExamRange。进步最大的学生是考试分数范围最大的学生。

     

要计算考试分数范围,必须从最高考试分数中减去最低考试分数。

     

例如,如果考试分数分别为90、75和84,则范围为90-75 = 15。

这是我添加了方法getExamRange()的Student类。

import java.util.*;

public class Student
{
    private static final int NUM_EXAMS = 4;

    private String firstName;
    private String lastName;
    private int gradeLevel;
    private double gpa;

    private int[] exams;
    private int numExamsTaken;
    public static int[] examRange = new int[Classroom.numStudentsAdded];
    private int i = 0;

    /**
     * This is a constructor.  A constructor is a method
     * that creates an object -- it creates an instance
     * of the class. What that means is it takes the input
     * parameters and sets the instance variables (or fields)
     * to the proper values.
     * 
     * Check out StudentTester.java for an example of how to use
     * this constructor. 
     */
    public Student(String fName, String lName, int grade)
    {
        firstName = fName;
        lastName = lName;
        gradeLevel = grade;
        exams = new int[NUM_EXAMS];
        numExamsTaken = 0;
    }

    public int getExamRange()
    {
        Arrays.sort(exams);
        examRange[i] = exams[exams.length-1] - exams[0];
        i++;
        return exams[exams.length-1] - exams[0];
    }

    public String getName()
    {
        return firstName + " " + lastName;
    }

    public void addExamScore(int score)
    {
        exams[numExamsTaken] = score;
        numExamsTaken++;
    }

    // This is a setter method to set the GPA for the Student.
    public void setGPA(double theGPA)
    {
        gpa = theGPA;
    }

    /**
     * This is a toString for the Student class. It returns a String
     * representation of the object, which includes the fields
     * in that object.
     */
    public String toString()
    {
        return firstName + " " + lastName + " is in grade: " + gradeLevel;
    }
}

这是我在其中添加方法getMostImprovedStudent()的Classroom类。

import java.util.*;

public class Classroom
{
    Student[] students;
    static int numStudentsAdded;

    public Classroom(int numStudents)
    {
        students = new Student[numStudents];
        numStudentsAdded = 0;
    }

    public Student getMostImprovedStudent()
    {
        Arrays.sort(Student.examRange);
        //return Student.examRange[0];
    }

    public void addStudent(Student s)
    {
        students[numStudentsAdded] = s;
        numStudentsAdded++;
    }

    public void printStudents()
    {
        for(int i = 0; i < numStudentsAdded; i++)
        {
            System.out.println(students[i]);
        }
    }
}

我可以通过对考试数组进行排序,然后从最大的数组中减去最小的数组来获得考试范围,但是一旦这样做,我如何找到考试范围最大的学生并返回?

2 个答案:

答案 0 :(得分:4)

您将执行此操作的方式是遍历onEnter={() => store.dispatch(isLoading(true))} ,并拥有一个变量以持有最大的分数差异和最进步的学生:

students

但是我们可以使用Java 8 +:

public Student getMostImprovedStudent()
{
    Student mostImproved = students[0];
    int biggest = student[i].getExamRange();
    for(int i = 1; i < students.length; i++) {
        if(students[i].getExamRange() > biggest) {
            mostImproved = students[i];
            biggest = students[i].getExamRange();
        }
    }
    return mostImproved;
}

假设public Student getMostImprovedStudent() { return Arrays.stream(students) .max(Comparator.comparing(Student::getExamRange)) .get(); } 不为空

答案 1 :(得分:3)

正如我在上面的评论中所解释的,您可以通过以下方式做到这一点:

  public Student getMostImprovedStudent() {
    Student maxRangeStudent = null;
    int maxRange = 0;
    for (Student student: students) {
      int curExamRange = student.getExamRange();
      if (curExamRange > maxRange){
        maxRangeStudent = student;
        maxRange = curExamRange;
      }
    }

    return maxRangeStudent;
  }