2012-09-21 130 views
4

我正在進行CS-101任務,並且只允許使用單個數組。我有一個數組,看起來像下面這樣:通過Java中的值對自定義對象數組排序

[Song, Song, Album, Fiction, Movie, Nonfiction, Song] 

這裏是背景層次(從我的任務要求):

「在你有一個名爲庫一類的頂級圖書館將有三個子類:音樂,書和電影,音樂將有兩個子類:歌曲和專輯,書中將有兩個子類:小說和非功能,電影,小說,非官方,歌曲, 和專輯將不會有任何子類。

我目前正試圖編寫一種方法,將按照ISBN號對圖書進行排序。所以小說和非小說是我的Book類的子類,它是Library的一個子類。

我持有Library myLibrary[] = new Library[100];

我不知道如何去僅從圖書檢索書號的,整理它們,因爲我只允許一個陣列的一切;否則我會很樂意製作一系列的書籍,然後將它們分開排序。

我可以利用什麼提示/算法來實現這一點?

更新

如果需要,我可以發佈更多的代碼。但是這個問題目前更側重於這種方法。

+1

你關心音樂和電影在陣列中的位置? – gtgaxiola

+0

不是在這一點上。我將有一個單獨的方法來排序這些。 – ardavis

回答

0

沒有試圖給實際執行的算法,你應該做一個就地排序,其中優先級可以通過完成:

書籍比音樂更優先和電影

2.如果兩個對象是書籍則優先級是根據ISBN

+0

因此,可能首先將書籍放在前面來整理整個圖書館?那麼如果它們是'instanceof Book',只需排序對象? – ardavis

+0

我的意思是,在排序時你已經把圖書移動到前面,看看原地Quicksort – gtgaxiola

+0

我把這個標記爲答案,因爲這是我用來做家庭作業的東西。其他答案也可能是正確的。 – ardavis

3

這裏的關鍵是正確地設置您的繼承和比實現Comparable接口。在這裏看到,例如:Java Comaprable和比調用的.sort你對你的父母類型的陣列上(在你的情況下,這將是myLibrary.sort();)下面是如何排序的基本類型工作的例子:Primitive type array sort

所以

  1. 您亞型實施Comaparable
  2. 創建父類型的數組,填充它
  3. 呼叫排序陣列上。

祝你好運!

1

檢查是否有效。 (目前在選項卡上,因此無法運行代碼)

[我認爲排序後,書籍將朝向陣列的一側飽和。請讓我知道結果]

/* book sorting is in decreasing order of ISBN, followed by non book items 
The books will be at the beginning of array, other items towards the end */ 
Arrays.sort(myLibrary, new Comparator<Library>() 
    { 
     int compare(Library l1, Library l2){ 
      //if both are books then compare ISBN and return appropriate 
      if((l1 instanceof Book) && (l2 instanceof Book)){ 
       Book b1=(Book)l1; Book b2=(Book)l2; 
       if(b1.getISBN()<b2.getISBN) { 
        return -1; 
       } else if(b1.getISBN()>b2.getISBN()) { 
        return 1; 
       } else { 
        return 0; 
       } 
      } 
      else {//if either one, or none are Book 

       //if only l1 is Book, l2 is not 
       if(l1 instanceof Book){ 
        return 1; 
       } 

       //if only l2 is Book, l1 is not 
       if(l2 instanceof Book){ 
        return -1; 
       } 

       //none are Book 
       return 0; 
      } 
     } 
    } 
); 
+0

那麼每本非書籍都等於每本書?這不可能是正確的。 (一方面,它違反了'x.compareTo(y)== 0'暗示所有'z'的'sgn(x.compareTo(z))== sgn(y.compareTo(z))'的要求。 ,讓'x'和'z'爲不同的書籍,讓'y'爲非書籍項目。)注意,這可以通過每當'l1'和'l2'是一個'Book'。 –

+0

但'l1,l2'是庫引用,'Library'沒有'''屬性;它特定於'Book'。 – SiB

+0

對不起,在比較ISBN號碼之前,庫實例需要轉換爲Book對象。 我編輯了代碼以反映更改。它現在可以工作了。 –

1

在這裏你去...

正如my previous answer提到撰寫新Comparator並使用相同的比較Library對象。

注:我沒有檢查爲空,但你應該這樣做...

class LibraryComparator implements Comparator<Library> { 
    public int compare(Library l1, Library l2){ 
     // If Both are Book instance do the comparison 
     if(l1 instanceof Book && l2 instanceof Book){ 
       // Assuming ISBN is a String or Long field in your class Book 
       return ((Book)l1).getISBN().compareTo(((Book)l2).getISBN()); 
     } else { 
     // Otherwise no change in ordering 
       return 0; 
       // You could specify sorting logic for Movie and Music here as well 
     } 
    } 
} 

然後你就可以像數組進行排序:一旦書籍進行分類做

Arrays.sort(myLibrary, new LibraryComparator()); 
相關問題