1

学生、教授、および部門としてのオブジェクトのリストを含む HashSet があります。どの学生の成績が最も高いか、どの学生の成績が最も高いかを合計で確認する必要があります。サンプルコードは次のように表します

  class Student{
    public String Name;
    public int Age;
    public int TotalMarks;
}
class Professor{
    public String Name;
    public int Age;
    public Student Student_Assigned;
}

class Department{
    Set<Professor> collectionDept = new HashSet<Professor>();

    public Student getStudentWithHigestMarks(){
        return null;
    }
}

Javaを使用してこれを見つけるにはどうすればよいですか?

4

4 に答える 4

2

を実装Comparableし、並べ替え/操作しますCollection

たとえば、メインコードでは次のようになります。

Student bad = new Student("bad");
bad.setMarks(2);
Student meh = new Student("meh");
meh.setMarks(5);
Student good = new Student("good");
good.setMarks(10);
Student otherGood = new Student("otherGood");
otherGood.setMarks(10);

// initializes a Set of students
Set<Student> students = new HashSet<Student>();
// adds the students
students.add(meh);
students.add(bad);
students.add(good);
students.add(otherGood);
// prints the "best student"
System.out.println(Collections.max(students).getName());
// initializing Set of best students
List<Student> bestStudents = new ArrayList<Student>();
// finding best mark
int bestMark = Collections.max(students).getMarks();
// adding to best students if has best mark
for (Student s: students) {
    if (s.getMarks() == bestMark) {
        bestStudents.add(s);
    }
}
// printing best students
for (Student s: bestStudents) {
    System.out.println(s.getName());
}

出力:

good
good
otherGood

...そして、これがあなたのStudentクラスのドラフトです:

public class Student implements Comparable<Student> {
    // we use encapsulation and set the fields' access to private
    private String name;
    private int age;
    private int marks;
    // we use encapsulation and have setters/getters/constructor access for the private fields
    public Student(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public int getMarks() {
        return marks;
    }
    public void setMarks(int marks) {
        this.marks = marks;
    }
    // TODO other setters/getters
    // here we implement the compareTo method and decide which int to return according to the "marks" field
    @Override
    public int compareTo(Student otherStudent) {
        if (marks < otherStudent.getMarks()) {
            return -1;
        }
        else if (marks > otherStudent.getMarks()) {
            return 1;
        }
        else {
            return 0;
        }
    }

また、インターフェイスのドキュメントを詳しく見てみるとよいでしょう。Comparable

于 2013-07-27T16:15:56.923 に答える
2

代わりに TreeSet を使用してください。Comparator を取るコンストラクターがあります。セットを自動的にソートします。

コンバータ:

Set<YourObject> hashSet = getItSomehow();
Set<YourObject> treeSet = new TreeSet<YourObject>(new YourComparator());
treeSet.addAll(hashSet);

ドキュメント: http://docs.oracle.com/javase/tutorial/collections/interfaces/order.html

于 2013-07-27T16:16:36.483 に答える
1

最高点の生徒の場合:

Student クラスと Professor クラスにComparableインターフェースを実装すると、標準の Java Collections.maxを簡単に使用できます。

Set<Student> students = ...;
Student highestMarks = Collections.max(students);

教授のcompareToメソッドに学生の最高点を含める場合は、次のようにします。

Professor professor = Collections.max(professors)
于 2013-07-27T16:09:18.363 に答える
1

これを使って:

public Student getStudentWithHigestMarks() {
    Student highest = null;
    for(Professor professor: collectionDept) {
        if(highest == null) {
            highest = professor.Student_Assigned;
        } else if(highest.TotalMarks < professor.Student_Assigned.TotalMarks) {
            highest = professor.Student_Assigned;
        }
    }
    return highest;
}
于 2013-07-27T16:12:33.530 に答える