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;
}
}
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.lang.Comparable;
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;
}
}
import java.util.*;
import java.lang.Comparable;
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
----------------解决方案--------------------------------------------------------
程序代码:
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);
}
}
}
----------------解决方案--------------------------------------------------------
嗯,谢啦。我慢慢研究一下
----------------解决方案--------------------------------------------------------
还是不行啊,你只帮我写了一下怎么比较价格,可是其它也需要比较啊。 可能还是得用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;
}
----------------解决方案--------------------------------------------------------
以下是引用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;
}
还是不行啊,你只帮我写了一下怎么比较价格,可是其它也需要比较啊。 可能还是得用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
不过注意这个比较对于含有中文字符的字符串不太理想,因为不是按照拼音来排序的
----------------解决方案--------------------------------------------------------
回复 4楼 vdestroyer
如果非要实现多种排序方法,可以考虑自己写排序方法,就是比较出大小后交换两者位置。也就是说不要用Comparable接口。
----------------解决方案--------------------------------------------------------