2017-05-08 7 views
1

私は自分のDS/Aの宿題に取り組んでいます。私の人生では、Comparable/Comparatorインターフェイスを使ってオブジェクトの特定のプロパティをキーとしてマークする方法を理解できません比較して注文する。コンパイラを使用してJavaでオブジェクトのプロパティを比較する

 incompatible types: Student[] cannot be converted to Comparable[] 

私は非常にいくつかの洞察力をいただければ幸いです:私たちは、私たちに提供されているマージソート機能を使用している、と私は私が受けています。このエラー以外の他のほとんどすべてのプログラムで作業を、持っていると信じてこの問題は、私はまだJavaとOOPをかなり新しくしています。ここで私が持っているものです:

TestMergeSort.java:

public class TestMergeSort { 

public static void main(String[] args) { 
    Student[] students = initializeStudentsArray(); 

    System.out.println("Displaying students array before sorting..."); 
    display(students); 

    System.out.println("Being sorting..."); 
    /* 
    * The Student array will be sorted by studentId 
    * which is declared as a String 
    */ 
    Merge.sort(students); // TODO: Fix the Student class to eliminate this error 
    System.out.println("End sorting..."); 

    System.out.println("Displaying students array after sorting..."); 
    display(students); 
} 

private static Student[] initializeStudentsArray() { 

    Student[] students = new Student[5]; 
    students[0] = new Student("Joe", "Jones", "1001"); 
    students[1] = new Student("Adam", "Ant", "950"); 
    students[2] = new Student("Bill", "Barnes", "735"); 
    students[3] = new Student("Mark", "Roth", "1102"); 
    students[4] = new Student("Jerome", "Howard", "1150"); 
    return students; 

} 

private static void display(Student[] students) { 
    for (Student std : students) { 
     System.out.println("Students [firstName=" + std.firstName + ", lastName=" + std.lastName + ", studentId=" + std.studentId + "]"); 
    } 
} 

}

とStudent.java:私はおそらくひどく何か間違ったことをやっている

public class Student implements Comparator<Student> { 

    public String firstName; 
    public String lastName; 
    public String studentId; 

    public Student(String first, String last, String Id) { 

     this.firstName = first; 
     this.lastName = last; 
     this.studentId = Id; 
    } 

    @Override 
    public int compare(Student stud1, Student stud2) { 

     String student1 = stud1.studentId; 
     String student2 = stud2.studentId; 

     return student1.compareTo(student2); 
    } 
} 

、そうしてください私に手がかりを入れてください。ありがとうございました!

+4

あなたの学生のクラスがComparableではなくコンパレータを実装する必要があります。 – dev4Fun

答えて

0
Merge.sort(students); 

私はMergeクラスが何であるかわかりません。私はちょうどデフォルトのCollections.sort(...)を使用します。

public class Student implements Comparator<Student> { 

それがデフォルトCollections.sortと同じように動作すると仮定すると、私はいつもコンパレータ、Comparableを実装していません。ここで

は、クラスのために、どのようにカスタムコンパレータを作成するComparable(ないコンパレータ)を実装する方法を示す例です:

/* 
** Use the Collections API to sort a List for you. 
** 
** When your class has a "natural" sort order you can implement 
** the Comparable interface. 
** 
** You can use an alternate sort order when you implement 
** a Comparator for your class. 
*/ 
import java.util.*; 

public class Person implements Comparable<Person> 
{ 
    String name; 
    int age; 

    public Person(String name, int age) 
    { 
     this.name = name; 
     this.age = age; 
    } 

    public String getName() 
    { 
     return name; 
    } 

    public int getAge() 
    { 
     return age; 
    } 

    public String toString() 
    { 
     return name + " : " + age; 
    } 

    /* 
    ** Implement the natural order for this class 
    */ 
    public int compareTo(Person p) 
    { 
     return getName().compareTo(p.getName()); 
    } 

    static class AgeComparator implements Comparator<Person> 
    { 
     public int compare(Person p1, Person p2) 
     { 
      return p1.getAge() - p2.getAge(); 
     } 
    } 

    public static void main(String[] args) 
    { 
     List<Person> people = new ArrayList<Person>(); 
     people.add(new Person("Homer", 38)); 
     people.add(new Person("Marge", 35)); 
     people.add(new Person("Bart", 15)); 
     people.add(new Person("Lisa", 13)); 

     // Sort by natural order 

     Collections.sort(people); 
     System.out.println("Sort by Natural order"); 
     System.out.println("\t" + people); 

     // Sort by reverse natural order 

     Collections.sort(people, Collections.reverseOrder()); 
     System.out.println("Sort by reverse natural order"); 
     System.out.println("\t" + people); 

     // Use a Comparator to sort by age 

     Collections.sort(people, new Person.AgeComparator()); 
     System.out.println("Sort using Age Comparator"); 
     System.out.println("\t" + people); 

     // Use a Comparator to sort by descending age 

     Collections.sort(people, Collections.reverseOrder(new Person.AgeComparator())); 
     System.out.println("Sort using Reverse Age Comparator"); 
     System.out.println("\t" + people); 

     // Use a Comparator with lambda expression to sort by age 

//  Collections.sort(people, (o1, o2) -> o1.getAge() - o2.getAge()); 
//  Collections.sort(people, Comparator.comparingInt(p -> p.getAge())); 
     Collections.sort(people, Comparator.comparingInt(Person::getAge)); 
     System.out.println("Sort using Lambda Age Comparator"); 
     System.out.println("\t" + people); 
    } 
} 
関連する問題