11

配列を生年順に並べ替えたいと思います。私の配列には、String 型の他の 2 つの要素があります。したがって、例として、1939 年などの最も古い年に生まれた人が一番上になり、その後も同様になります。

これが私のコードです:

import java.util.*;
public class StudentInformationTest
{
public static void main (String [] args){ 
    StudentInformation[] studentInfo = new StudentInformation[10];

    studentInfo[0] = new StudentInformation("Student A",1971, "BSc FDIT");
    studentInfo[1] = new StudentInformation("Student B",1964, "BSc FDIT"); 
    studentInfo[2] = new StudentInformation("Student C",1996, "BSc FDIT"); 
    studentInfo[3] = new StudentInformation("Student D",1939, "BSc FDIT"); 
    studentInfo[4] = new StudentInformation("Student E",1945, "BSc FDIT"); 
    studentInfo[5] = new StudentInformation("Student F",1991, "BSc FDIT"); 
    studentInfo[6] = new StudentInformation("Student G",1987, "BSc FDIT"); 
    studentInfo[7] = new StudentInformation("Student H",1968, "BSc FDIT"); 
    studentInfo[8] = new StudentInformation("Student I",1968, "BSc FDIT"); 
    studentInfo[9] = new StudentInformation("Student J",1973, "BSc FDIT"); 

    printInfo(studentInfo);
    printAge(studentInfo);
}
public static void printInfo(StudentInformation studentInfo[]){
    for(int i = 0; i < studentInfo.length; i++){
        System.out.println(studentInfo[i].getStudentName() + " " +   studentInfo[i].getBirthDate() + " " + studentInfo[i].getProgrammeOfStudy());
    }
    System.out.println();
}

 }

}

生年月日を降順に出力できたら、学生の名前と彼らが行っている大学のモジュールも表示する必要があります。これを行う方法について他の質問が寄せられていることは知っていますが、他のオブジェクトを含むものを見ることができませんでした。これはクラス セッションですので、私のコードに誤りがあればご容赦ください。

4

3 に答える 3

32

とを使用しComparatorますArrayList

Java8の場合

Comparator!で新しいデフォルトメソッドと静的メソッドを使用します。

ArrayList<StudentInformation> infos = new ArrayList<StudentInformation>();
// fill array
Collections.sort(infos, 
    Comparator.comparingInt(StudentInformation::getBirthYear).reversed());

それは勇敢な新しい世界です!:)

または、Java 7よりも優れていますが、ラムダを使用してください。

ArrayList<StudentInformation> infos = new ArrayList<StudentInformation>();
// fill array
Collections.sort(infos, (s1, s2) ->
    Integer.compare(s2.getBirthYear(), s1.getBirthYear()));

Java7の場合

匿名の内部クラスを使用します。

class StudentDateComparator implements Comparator<StudentInformation> {
    public int compare(StudentInformation s1, StudentInformation s2) {
        return Integer.compare(s2.getBirthYear(), s1.getBirthYear());
    }
}

ArrayList<StudentInformation> infos = new ArrayList<StudentInformation>();
// fill array
Collections.sort(infos, new StudentDateComparator());

説明

これComparatorにより、特定のタイプ(この場合は)の2つのオブジェクトを比較できますStudentInformationStudentInformation実装を作成することもできますがComparable<StudentInformation>、学生情報を比較する方法が複数あるため、この方法の方がおそらく優れています(日付ごと、ここのように、名前、名前、登録されているクラスの数など)。

s1コンパレータのとの順序を入れ替えることによりs2、逆の順序を誘導します。これを行う別の方法compareは、通常の順序で呼び出しを無効にするか、通常のコンパレータを使用してそれをでラップすることですCollections.reverseOrder


これは、標準のアレイを使用して行うこともできます。

StudentInformation[] infos = new StudentInformation[10];
// fill array
Arrays.sort(infos, new StudentDateComparator());
于 2013-03-10T19:09:25.130 に答える
1

Java ランドには、次のComparable<E> インターフェースがあります。

interface Comparable<E> {
  public int compareTo(E other);
}

StudentInfoimplementを作成し、要件に従って実装し、標準ライブラリのメソッドを使用するか (代わりに a が必要になりComparable<StudentInfo>ます)、またはケースに適した並べ替えアルゴリズムを使用する必要があります。compareTo(StudentInfo other)Comparator

于 2013-03-10T19:12:40.160 に答える
1

StudentInformationクラスを実装させることができますComparable。次に、メソッドを実装する必要がありますcompareTo。このメソッドは、クラスの 2 つのオブジェクトが等しいか、小さいか大きいかをいつ、どのように扱うかを定義します。

このプロパティを明確に定義すると、Collections.sortutility を使用してコレクションを並べ替えることができます。

于 2013-03-10T19:14:06.270 に答える