2012-09-21 132 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()); 
相关问题