亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我不明白我們如何在方法 compareTo 中使用方法 compareTo

我不明白我們如何在方法 compareTo 中使用方法 compareTo

阿晨1998 2023-03-23 14:21:04
我正在做這個項目,在那里我會得到一些書,我應該按照這個邏輯對它進行排序:如果一本書的頁數比另一本書的頁數多,那么頁數多的書排在第一位。如果兩本書的頁數相同,則按標題字母順序排序。如果兩本書的頁數和標題相同,則按作者字母順序排序。我試圖解決但沒有成功,所以我在解決方案中查找:public class Book implements Comparable<Book>{   public int compareTo(Book specifiedBook) {      // First check if they have different page counts      if(this.numberOfPages != specifiedBook.numberOfPages){         // this will return a negative value if this < specified but will return a positive value if this > specified         return this.numberOfPages - specifiedBook.numberOfPages;      }      // If page counts are identical then check if the titles are different      if(!this.title.equals(specifiedBook.title){         return this.title.compareTo(specifiedBook.title);      }      // If page titles are also identical then return the comparison of the authors      return this.author.compareTo(specifiedBook.author);    }}我在這里不明白的是這部分“return this.title.compareTo(specifiedBook.title);”。我們如何在方法 compareTo 中使用方法 compareTo,這不是遞歸嗎,我知道當我們實現接口 comperable 時,我們必須覆蓋方法 compareTo,但是我們如何在覆蓋的 compareTo 方法中使用該 compareTo 方法?在這種情況下和一般情況下,它應該做什么?令我困惑的是,我們如何使用剛剛在接口中聲明的方法,它不是從父類繼承的。它只是在可比接口中聲明,所以當我們實現接口時,我們必須重寫它
查看完整描述

2 回答

?
慕虎7371278

TA貢獻1802條經驗 獲得超4個贊

假設titleauthor是字符串,您正在String.compareTo從內部調用(已經實現)Book.compareTo。這不是遞歸。這是不同類中的方法。



查看完整回答
反對 回復 2023-03-23
?
SMILET

TA貢獻1796條經驗 獲得超4個贊

使用 java.util.Comparator 比較字段的最簡單和更好的方法。我強烈建議覆蓋 equals 和 hashCode 方法,因為您要將書籍放在某個容器中,該容器可能是 Sorted Set 或類似的東西。無論如何,這是示例代碼,


import org.junit.Test;


import java.util.Comparator;

import java.util.Objects;

import java.util.StringJoiner;

import java.util.TreeSet;


public class BookTest {


  @Test

  public void compareBooks() {

    Book b1 = new Book(100, "A book", "Zoro");

    Book b2 = new Book(10, "Small book", "ABC");

    TreeSet<Book> books = new TreeSet<>();

    books.add(b1);

    books.add(b2);

    System.out.println(books);

  }


  private class Book implements Comparable<Book> {

    private final int numberOfPages;

    private final String title;

    private final String author;


    private Book(int numberOfPages, String title, String author) {

      this.numberOfPages = numberOfPages;

      this.title = title;

      this.author = author;

    }


    public int getNumberOfPages() {

      return numberOfPages;

    }


    public String getTitle() {

      return title;

    }


    public String getAuthor() {

      return author;

    }


    @Override

    public int compareTo(Book that) {

      return Comparator.nullsFirst(

              Comparator.comparing(Book::getNumberOfPages)

                  .thenComparing(Book::getTitle)

                  .thenComparing(Book::getAuthor))

          .compare(this, that);

    }


    @Override

    public boolean equals(Object o) {

      if (this == o) return true;

      if (o == null || getClass() != o.getClass()) return false;

      Book book = (Book) o;

      return numberOfPages == book.numberOfPages

          && title.equals(book.title)

          && author.equals(book.author);

    }


    @Override

    public int hashCode() {

      return Objects.hash(numberOfPages, title, author);

    }


    @Override

    public String toString() {

      return new StringJoiner(", ", Book.class.getSimpleName() + "[", "]")

          .add("numberOfPages=" + numberOfPages)

          .add("title='" + title + "'")

          .add("author='" + author + "'")

          .toString();

    }

  }

}



查看完整回答
反對 回復 2023-03-23
  • 2 回答
  • 0 關注
  • 128 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號