2014-02-25 75 views
2

我有一個Faculty成員的數組,我想使用插入排序它。 我在得到一個錯誤:用對象數組插入排序?

InsertionSort.insertionSortA(faculty, faculty.length); 

錯誤說:「方法insertionSortA類插入排序不能用於給定類型;
要求:可比[],詮釋
發現:教師[],INT
我知道,這樣做是行不通的:

InsertionSort.insertionSortA((Comparable[]) faculty, faculty.length); 

我知道,如果我有一個Integer []數組的工作,但我很困惑,爲什麼我的教師[] WOU ld不工作?

public class Tester { 

public static void main(String[] args) { 

    InsertionSort insertionSort = new InsertionSort(); 
    Education edu = new Education ("BA", "Business", 1); 
    Education edu2 = new Education ("BA", "Health Science", 1); 
    Education edu3 = new Education ("BA", "Computer Science", 1); 

    Faculty[] faculty = new Faculty[] { 

    new Faculty ("538", "Doe", "Jane", 'M', 1994,1,10, 
    "Assistant", edu), 
    new Faculty ("238", "Do", "John", 'F', 1994,6,1, 
    "Assistant", edu2), 
    new Faculty ("080", "White", "Snow", 'F', 1994,4,22, 
    "Full", edu3) 
    }; 

    InsertionSort.insertionSortA(faculty, faculty.length); 
} 

}

public class InsertionSort { 

public static void insertionSortA(Comparable[] theArray, int n) { 

    for (int unsorted = 1; unsorted < n; ++unsorted) { 
     Comparable nextItem = theArray[unsorted]; 
     int loc = unsorted; 
     while ((loc > 0) &&(theArray[loc-1].compareTo(nextItem) > 0)) { 
      theArray[loc] = theArray[loc-1]; 
      loc--; 
     } // end while 
     theArray[loc] = nextItem; 
    } // end for 
} // end insertionSort 
public static void insertionSortB(Comparable[] theArray, int n) { 

    for (int unsorted = 1; unsorted < n; ++unsorted) { 
     Comparable nextItem = theArray[unsorted]; 
     int loc = unsorted; 
     while ((loc > 0) &&(theArray[loc-1].compareTo(nextItem) < 0)) { 
      theArray[loc] = theArray[loc-1]; 
      loc--; 
     } // end while 
     theArray[loc] = nextItem; 
    } // end for 
} 
} 

回答

3

爲了應用插入排序在您的陣列/集合的元素必須是可比的(比較的邏輯是你要如何比較2名教職員工的基礎上,可以基於姓名,年齡,工資等)。

public class Faculty implements Comparable<Faculty> 
{ 
    public int compareTo(Faculty f) 
    { 

    // comparison logic is on the basis of how you want to compare 2 faculty members 
    // you might want to compare name, salaries etc 

    } 
} 

的compareTo()函數必須返回以下:

zero    : If object is same as the specified object (f) 
positive integer : If object is greater than the specified object (f) 
negative integer : If object is less than the specified object (f) 

在參考文檔這可以通過實現接口學院類可比和定義函數的compareTo()如下進行以下link

2

沒有看到Faculty類,我們只能猜測你的問題。

看來Faculty沒有實現Comparable。您可以使用Integer[]調用函數,因爲Integer可比較 - 它確實實施Comparable

您需要實現Comparable<Faculty>Faculty類,通過重寫compareTo(Faculty)

public int compareTo(Faculty faculty) { 
    //... 
} 

至於這應該返回什麼,你應審查其API。但一般情況下,如果它們完全相等,則返回0,如果this大於faculty(無論定義「更大」的意思),它應該返回大於0的值。除此之外沒有任何嚴格的規則除了它總是返回一個一致的值。

0

對於那些仍然在這個頁面中磕磕絆絆的人來說,另一種解決方案是忽略Comparable接口。由於在很多情況下,對象可以通過多個屬性進行排序,例如id,first name,last name或start year。在這種情況下,您可以爲InsertionSort類中的每個排序屬性創建單獨的方法。這還需要您將Sortrable方法輸入參數類型從Comparable []更改爲Faculty []。

public class InsertionSort { 

    public static void byLastName(Faculty[] theArray, int n) { 

    for (int unsorted = 1; unsorted < n; ++unsorted) { 
     Faculty nextItem = theArray[unsorted]; 
     int loc = unsorted; 
     while ((loc > 0) &&(theArray[loc-1].getLastName().compareTo(nextItem.getLastName()) > 0)) { 
     theArray[loc] = theArray[loc-1]; 
     loc--; 
     } 
     theArray[loc] = nextItem; 
    } 
    } 

    public static void byFirstName(Faculty[] theArray, int n) { 

    for (int unsorted = 1; unsorted < n; ++unsorted) { 
     Faculty nextItem = theArray[unsorted]; 
     int loc = unsorted; 
     while ((loc > 0) &&(theArray[loc-1].getFirstName().compareTo(nextItem.getFirstName()) > 0)) { 
     theArray[loc] = theArray[loc-1]; 
     loc--; 
     } 
     theArray[loc] = nextItem; 
    } 
    } 
} 

然後,您可以整理你的學院陣列像這樣:

InsertionSort.byLastName(faculty, faculty.length); 

OR

InsertionSort.byFirstName(faculty, faculty.length); 

有關使用插入排序對象進行排序,檢查出我blogpost更詳細的解釋。它具有Java,C++,Python和Javascript的實現。