public class Publication {
private String name;
private String author;
private int price;
private String language;}
public class Book extends Publication {
private int ISBN;
public Book(String name, String author, String language, int price, int ISBN) {
super(name, author, language, price);
this.ISBN = ISBN;
}
.
public class Book extends Publication {
private int ISBN;
public Book(String name, String author, String language, int price, int ISBN) {
super(name, author, language, price);
this.ISBN = ISBN;
}
考慮這些課程
在另一個類中,我制作了一個類的陣列Book串列
現在我想根據name然后排序和列印這個陣列串列,author
但我不知道我能做什么
uj5u.com熱心網友回復:
List<Book> books = new ArrayList<>();
//put elements in your list
books.sort((b1,b2) ->{
int comparator;
if (b1.name==b2.name){
comparator = b1.author.compareTo(b2.author);
} else {
comparator = b1.name.compareTo(b2.name);
}
return comparator;
});
在 list.short( (b1,b2) ); 是如何 b1 是當前“書”,而 b2 是串列中的下一個“書”,回傳值定義 b1 是在 b2 之前還是之后;
b1 和 b2 是 Book 型別的變數,您可以更改它們的名稱。
注意:String.compareTo() 方法;按字母順序排序,但優先考慮大寫字母,例如:它將大寫“Z”排在小寫“a”之前。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/464914.html
下一篇:具有不同引數的Python類繼承
