2015-07-01 39 views
1

我想知道爲什麼在嘗試排序列表後出現錯誤。 當我嘗試對包含「學生」對象的列表進行排序時發生錯誤。使用Collections.sort時發生錯誤()

import java.lang.reflect.Array; 
import java.util.*; 
import java.util.ArrayList; 

public class mainProgram { 
    public static void main(String[] args) { 
     List<Student> classStudents = new ArrayList<Student>(); 

    //Add 4 students to List 
     classStudents.add(new Student("Charles", 22)); 
     classStudents.add(new Student("Chris", 25)); 
     classStudents.add(new Student("Robert", 23)); 
     classStudents.add(new Student("Adam", 21)); 

    //sort and print 
     Collections.sort(classStudents); //Why does this not work? 
     System.out.println("classStudent(Sorted) ---> " 
     + Arrays.toString(classStudents.toArray())); 
    } 
} 

class Student implements Comparable<Student>{ 
    // fields 
    private String name; 
    private int age; 

    //Constructor 
    public Student(String name, int age){ 
     name = name; 
     age = age; 
     } 
    //Override compare 
    public int CompareTo(Student otherStudent){ 
     if(this.age == otherStudent.age){ 
      return 0; 
     } else if(this.age < otherStudent.temp){ 
      return -1; 
     } else{ 
      return 1; 
     } 
    } 

    //Override toString 
    public String toString(){ 
     return this.name + " " + this.age; 
    } 
} 

interface Comparable<Student>{ 
    int CompareTo(Student otherStudent); 
    String toString(); 
} 

的錯誤是:

Bound mismatch: The generic method sort(List<T>) of type Collections is not applicable for the arguments (List<Student>). The inferred type Student is not a valid substitute for the bounded parameter <T extends Comparable<? super T>> 

我不明白什麼是錯誤的手段和如何解決它。您的意見將非常感謝。

回答

8

它可能不是那麼明顯了基於錯誤信息,但如果你在documentation of Collection.sort看看您填寫發現,這種方法(如標準的API方法休息)預計名單實施的實例已經在API接口中定義,其在這種情況下是java.lang.Comparable

所以不要嘗試創建自己的Comparable接口。請記住,如果某些方法需要某種類型,則此類型必須已經在某處定義(否則,使用此方法的類將不會編譯,因爲方法不能使用某些未知/未定義的類型)。

另外在java.util.Comparable中定義的比較方法是compareTo而不是CompareTo(Java區分大小寫,因此這些方法不會被視爲相等)。


其他問題是

  • Student沒有temp字段,以便

    } else if (this.age < otherStudent.temp) { 
    

    也許應該

    } else if (this.age < otherStudent.age) { 
    

    甚至貝特R,而不是這個age比較條件if(..<..){-1} else if(..==..){0} else {1}只需使用

    return Integer.compare(age, otherStudent.age); 
    
  • 在構造函數中

    需要this.name確定哪些name你指的。 this.name表示此實例的字段,name是對傳遞給構造函數的變量的引用。因此,你需要

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

Java定義了自己的Comparable接口。刪除你的和導入theres,集合方法使用java之一。

0

首先,您會覆蓋Comparable接口方法「compareTo()」 - 它應該是小寫的「c」,而不是大寫。第二,我不知道你想用最後一個比特開始做什麼,「接口比較」。放下那部分 - 你不需要它。

Comparable接口是API的一部分。

乾杯。

1

一些小的修改,看看:

MainProgram.java

import java.util.ArrayList; 
    import java.util.Arrays; 
    import java.util.Collections; 
    import java.util.List; 

    public class MainProgram { 

     public static void main(String[] args) { 
      System.out.println("HI !!!"); 
      List<Student> classStudents = new ArrayList<Student>(); 

     //Add 4 students to List 
      classStudents.add(new Student("Charles", 22)); 
      classStudents.add(new Student("Chris", 25)); 
      classStudents.add(new Student("Robert", 23)); 
      classStudents.add(new Student("Adam", 21)); 

     //sort and print 
      Collections.sort(classStudents); //Why does this not work? 
      System.out.println("classStudent(Sorted) ---> " 
      + Arrays.toString(classStudents.toArray())); 
     } 
    } 

Student.java

class Student implements java.lang.Comparable<Student>{ 
    // fields 
    private String name; 
    private int age; 

    //Constructor 
    public Student(String name, int age){ 
     this.name = name; 
     this.age = age; 
     } 

    //Override compare 
    public int compareTo(Student otherStudent){ 
     if(this.age == otherStudent.age){ 
      return 0; 
     } else if(this.age < otherStudent.age){ 
      return -1; 
     } else{ 
      return 1; 
     } 
    } 

    //Override toString 
    public String toString(){ 
     return this.name + " " + this.age; 
    } 
} 

輸出: classStudent(排序) - - [Adam 21,Charles 22,Robert 23,Chris 25]