【发布时间】:2021-04-19 02:32:25
【问题描述】:
问题:定义一个名为 Bookshelf 的类,它只包含一个 main() 方法。 Bookshelf 类必须创建一打 (12) 个具有不同属性的 Book 对象,并且必须将它们存储在 Books 的 ArrayList 中。
Bookshelf 类必须按照它们进入 ArrayList 的顺序列出 ArrayList 中所有书籍的所有属性。为 Bookshelf 创建一个排序函数,它将 ArrayList 中的书籍按名称升序排序,然后按出版年份排序。
提示:您需要定义一个比较器类,它将两个 Book 对象作为 compareTo 方法的参数。此方法应进行两步比较并返回布尔值。第一次比较应该比较书名。如果名称相同,则第二次比较应比较出版年份。
将代码添加到 main() 以在第一个输出列表之后显示排序后的图书列表 - 即按照它们进入 ArrayList 的顺序显示图书。
到目前为止,除了最后一部分,我已经完成了作业。我的代码如下所示:
public class Book implements Comparable<Book> {
private String title;
private String isbnNumber;
private String author;
private String edition;
private String publisher;
private String yearPublished;
public Book(String title, String isbnNumber, String author, String edition, String publisher, String yearPublished) {
this.title = title;
this.isbnNumber = isbnNumber;
this.author = author;
this.edition = edition;
this.publisher = publisher;
this.yearPublished = yearPublished;
}
public String getTitle() { constructor.
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getIsbnNumber() {
return isbnNumber;
}
public void setIsbnNumber(String isbnNumber) {
this.isbnNumber = isbnNumber;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getEdition() {
return edition;
}
public void setEdition(String edition) {
this.edition = edition;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getYearPublished() {
return yearPublished;
}
public void setYearPublished(String yearPublished) {
this.yearPublished = yearPublished;
}
public int compareTo(Book o1, Book o2){
if(o1.getTitle().compareTo(o2.getTitle()) <0){
System.out.println(o1.getTitle());
} else if(o1.getTitle().compareTo(o2.getTitle()) > 0){
System.out.println(o2.getTitle());
} else if(o1.compareTo(o2) == 0){
if(o1.getYearPublished().compareTo(o2.getYearPublished()) <0){
System.out.println(o1.getTitle() + " " + o1.getYearPublished());
} else {
System.out.println(o2.getTitle() + " " + o2.getYearPublished());
}
}}
}
public class BookShelf {
public static void main(String[] args) {
Book[] arrList = {
new Book("My wife said you may want to marry me", "978-0-06-294059-9", "Jason B Rosenthal", "first",
"HarperCollins", "2020"),
new Book("The Archaic Revival", "978-0-06-250613-9", "Terrence McKenna", "first",
"HarperCollins", "1991"),
new Book("Thirteen", "978-1-4091-7076-9", "Steve Cavanagh", "first", "Orion", "2018"),
new Book("The Gas We Pass", "978-1-929132-15-7", "Shinta Chow", "first", "Kane/Miller", "1994"),
new Book("A Porcupine in a Pine Tree", "978-0-545-98663-2", "Helaine Becker", "first", "Scholastic", "2010"),
new Book("Eddie's Monster", "0-89660-102-1", "Michael Abrams", "first", "Abbeville", "1996"),
new Book("Harry Potter and the Philospher's Stone", "1-55192-398-x", "J.K. Rowling", "second", "Raincoast Books", "2000"),
new Book("Purple, Green and Yellow", "1-55037-256-4", "Robert Munsch", "sixth", "Annick Press", "1992"),
new Book("Franklin's Blanket", "1-55074-278-7", "Paulette Bourgeois", "first", "Kids Can Press", "1995"),
new Book("Franklin Has a Sleepover", "1-55074-302-3", "Paulette Bourgeois", "first", "Kids Can Press", "1996"),
new Book("Proud", "1-55285-274-1", "Fred Penner", "first", "Whitecap Books", "1997"),
new Book("Goodnight, Little Hare", "978-1-84895-560-8", "Sheridan Cain", "second", "Little Tiger Press", "1999")};
System.out.println("****BOOKSHELF****\n");
for(int i=0;i<arrList.length;i++){
System.out.println(arrList[i].getTitle() + ", " + arrList[i].getIsbnNumber() + ", " + arrList[i].getAuthor() + ", " + arrList[i].getEdition() + ", "
+ arrList[i].getPublisher() + ", " + arrList[i].getYearPublished()+ '\n');
for(int i = 0; i<arrList.length;i++){
compareTo(arrList[i], arrList[i+1]);
}
}}
}
我不知道如何根据标题对数组进行排序,然后发布年份并将其打印出来。
【问题讨论】:
-
您是否在 Google 上搜索过使用自定义
Comparator进行排序?提示会告诉您需要做什么,因此我建议您找到有关自定义比较器的教程并尝试一下。我看到你确实有一个compareTo()方法,但重要的是要注意Comparable.compareTo()与Comparator.compareTo()相似但不同。确保查找后者。 -
糟糕的标题。重写以总结您的具体技术问题。
标签: java comparator