2017-05-08 146 views
1

我正在處理我的DS/A作業,但我不能爲我的生活弄清楚如何使用Comparable/Comparator接口將我的Object的特定屬性標記爲關鍵字通過比較和排序。我們使用的是已經提供給我們一個合併功能,我相信我有大部分一切在計劃工作,比這個錯誤我收到其他:使用比較器來比較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

您的學生類實現可比,而不是比較。 – dev4Fun

回答

0
Merge.sort(students); 

我不知道合併類是什麼。我只使用默認的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); 
    } 
}