| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2329 人关注过本帖
标题:ArrayList本身又包含ArrayList,如何排序?
只看楼主 加入收藏
vdestroyer
Rank: 2
等 级:论坛游民
帖 子:136
专家分:14
注 册:2009-1-7
结帖率:96.43%
收藏
已结贴  问题点数:20 回复次数:5 
ArrayList本身又包含ArrayList,如何排序?
对程序的要求大概是这样的:先建立一个类,Book。这个类包含 书名,ISBN,版本,价格,还有作者。一本书可以有很多个作者,所以这些作者,是要用ArrayList来存。
另一个类,CollectionOfBooks,这个类与Book的关系也是ArrayList。这个类可以对这些Book进行不同方式的排序,按照书名,按照ISBN等等。
我现在的问题是我不知道怎么实现不同方式的排序。
老师说用Generics比较容易些,可是那个对我来说有些太复杂。最好有哪位能不用Generics的。还有就是怎么写compareTo,在这儿也没有思路了。。
另外,老师还说可以用Collection.sort(book) 来实现排序,这个我试了,确实可以。可问题是这个只能按照一种方式排序,是不是我要重写这个sort?如果是,怎么写?(我java基础比较差,理解的不多。虽然是问作业,但真的不是自己不学,只是老师讲得太快了。大家能给个思路就好,不用帮忙写代码)
程序代码:
package laboration03_del02;

import java.util.*;
import java.lang.*;
public class Book implements Comparable <Book>
{
    private String isbn, title;
    private int edition;
    private double price;
    //private Author[] author = new Author[10];
    ArrayList <String> author = new ArrayList<String>();
   
    public Book(String isbn, String title, int edition, double price){
       
        this.isbn = isbn;
        this.title = title;
        this.edition = edition;
        this.price = price;
    }
   
    public ArrayList<String> getAuthors(){
        //a.toArray();
        return author;
    }
   
    public void addAuthor(String author_){
        author.add(author_);
    }
   
    public String getTitle()
    {
        return title;
    }
   
    public String getISBN()
    {
        return isbn;
    }
   
    /* 我自己写的equals 和compareTo,当然都不对,也不知道怎么写*/
    public int compareTo(Book other) {
        String tit = this.title;
        String othertitle = other.title;
        boolean same = tit.equals(othertitle);
        if(same == true) return 0;
        else return 1;
    }
   
    public boolean equals(Object other)
    {
        if(!(other instanceof Book))
        {
            return false;
        }
        Book b = (Book)other;
        return b.title.equals(title);
    }
    
    public String toString() {
        String info = "ISBN: " + isbn + "\nTitle: " + title +
                      "\nEdition: " + edition + "\nPrice: " + price + "\n==========";
        return info;
    }

   
} 



程序代码:
package laboration03_del02;

import java.util.*;
import java.public class CollectionOfBooks<T> implements Comparable<Book>, Comparator<Book>
{
    //Book[] books = new Book[100];
    ArrayList<Book> book = new ArrayList<Book>();
   
    public void addBook(Book book)
    {
        this.book.add(book);
    }
   
    public void removeBook(Book book)
    {
        this.book.remove(book);
    }
   
    public ArrayList<Book> getBooksByTitle()
    {
        Collections.sort(book);
        //sortObjects(book);
        return book;
    }
   
    public ArrayList<Book> getBooksByAuthor()
    {
        Collections.sort(book);
        return book;
    }
   
    public ArrayList<Book> getBooksByISBN()
    {
        Collections.sort(book);
        return book;
    }
   
    /*
    public static <T extends Comparable <T>> void sort(List<T>[] arr)
    {
        int minindex;
        List<T> temp;
        System.out.println("Here");
        for(int i = 0; i < arr.length - 1; i++)
        {
            minindex = i;
            for(int j = i + 1; j < arr.length; j++)
            {
                //if(arr[j].compareTo(arr[minindex]) < 0)
                if(((Comparable<T>) arr[j]).compareTo((T) arr[minindex]) < 0)
                {
                    minindex = j;
                }
               
                temp = arr[i];
                arr[i] = arr[minindex];
                arr[minindex] = temp;
            }
        }
    }
     */
   
    /* 我自己写的equals 和compareTo,当然都不对,也不知道怎么写*/
    public boolean equals(Book other)
    {
        System.out.println("-----------Called equals");
        Book b = null;
        String tit = b.getTitle();
        return tit.equals(other);
    }
   
    public int compareTo(Book other)
    {
        System.out.println("-----------Called compareTo");
        int res = 0;
        //this.title.equals(other.title);
        return res;
    }
   
   
    public void print()
    {
        for(Book s : book)
        {
            System.out.println(s);
        }
    }
   
    public String toString()
    {
        String info = new String();
        info = "There are " + book.size() + " book(s)";
        return info;
    }

   
    public int compare(Book arg0, Book arg1) {
       
        return 0;
    }
    

}



[ 本帖最后由 vdestroyer 于 2010-9-27 16:54 编辑 ]
搜索更多相关主题的帖子: ArrayList 
2010-09-27 15:12
shellingford
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:19
帖 子:228
专家分:1348
注 册:2010-8-9
收藏
得分:10 
程序代码:
public class Book implements Comparable <Book>
{
    private String isbn, title;
    private int edition;
    private double price;
    //private Author[] author = new Author[10];
    ArrayList <String> author = new ArrayList<String>();
   
    public Book(String isbn, String title, int edition, double price){
       
        this.isbn = isbn;
        this.title = title;
        this.edition = edition;
        this.price = price;
    }
   
    public ArrayList<String> getAuthors(){
        //a.toArray();
        return author;
    }
   
    public void addAuthor(String author_){
        author.add(author_);
    }
   
    public String getTitle()
    {
        return title;
    }
   
    public String getISBN()
    {
        return isbn;
    }

    @Override
    public int compareTo(Book o) { //实现排序的方法,这里是根据价格排序
        // TODO Auto-generated method stub
        if(o.getPrice()>this.price){
            return -1;
        }else if(o.getPrice()<this.price){
            return 1;
        }
        return 0;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
    
    public static void main(String[] args) {
        List<Book> list=new ArrayList<Book>();
        Random ra=new Random();
        for (int i = 0; i < 10; i++) {
            Book book=new Book("","",1,ra.nextDouble());    //随机生成10本不同价格的书
            list.add(book);
        }
        Collections.sort(list);        //排序
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).price);    
        }
    }
}
2010-09-27 17:00
vdestroyer
Rank: 2
等 级:论坛游民
帖 子:136
专家分:14
注 册:2009-1-7
收藏
得分:0 
嗯,谢啦。我慢慢研究一下
2010-09-27 17:17
vdestroyer
Rank: 2
等 级:论坛游民
帖 子:136
专家分:14
注 册:2009-1-7
收藏
得分:0 
还是不行啊,你只帮我写了一下怎么比较价格,可是其它也需要比较啊。 可能还是得用Generics
如果是比较ISBN,我觉得大概可以这么写,可ISBN是字符串(虽然都是用数字组成的),应该用equals吧?可是equals返回的是true false啊,怎么办?
    public int compareTo(Book other)
    {
        Book b = null;
        if(other.getISBN().equals(b.getISBN()))
        {
            return -1;
        }
        else if(other.getISBN().equals(b.getISBN()))
        {
            return 1;
        }
        return 0;
    }
2010-09-27 18:50
shellingford
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:19
帖 子:228
专家分:1348
注 册:2010-8-9
收藏
得分:5 
以下是引用vdestroyer在2010-9-27 18:50:30的发言:

还是不行啊,你只帮我写了一下怎么比较价格,可是其它也需要比较啊。 可能还是得用Generics
如果是比较ISBN,我觉得大概可以这么写,可ISBN是字符串(虽然都是用数字组成的),应该用equals吧?可是equals返回的是true false啊,怎么办?
    public int compareTo(Book other)
    {
        Book b = null;
        if(other.getISBN().equals(b.getISBN()))
        {
            return -1;
        }
        else if(other.getISBN().equals(b.getISBN()))
        {
            return 1;
        }
        return 0;
    }


equals只是比较是否相等,所以返回自然是true false,他不能比较谁大谁小的。
字符串大小比较:String类型也是有compareTo方法的,返回是int
不过注意这个比较对于含有中文字符的字符串不太理想,因为不是按照拼音来排序的
2010-09-28 09:34
shellingford
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:19
帖 子:228
专家分:1348
注 册:2010-8-9
收藏
得分:5 
回复 4楼 vdestroyer
如果非要实现多种排序方法,可以考虑自己写排序方法,就是比较出大小后交换两者位置。
也就是说不要用Comparable接口。
2010-09-28 09:36
快速回复:ArrayList本身又包含ArrayList,如何排序?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020303 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved