对 ArrayList 进行排序:“不存在类型变量 T 的实例,因此 bookList 符合 Comparable<? super T>”

问题描述 投票:0回答:2

我创建了一个书单的 Java 类。

public class bookList {
    public String title, author, genre;
    public bookList (String beginTitle, String beginAuthor, String beginGenre)
    {
        title = beginTitle;
        author = beginAuthor;
        genre = beginGenre;
    }
    public String getTitle(){return title;}
    public String getAuthor(){return author;}
    public String getGenre(){ return genre;}
    public void setTitle(String titleUpdate){title = titleUpdate;}
    public void setAuthor(String authorUpdate){author = authorUpdate;}
    public void setPubDate(String genreUpdate){genre = genreUpdate;}

    @Override
    public String toString() {
        return "title: " + title + '\t'+
                "author: " + author + '\t'+
                "genre: " + genre +
                 "\n";
    }
}

然后我创建了一个驱动程序类,将书籍添加到 toRead ArrayList 中。

import java.util.*;
public class Main {
    public static void main(String[] args) {
        int userResponse = 1;
        String title, author, genre;
        String userInput;
        Scanner scan = new Scanner(System.in);
        bookList newBook = new bookList(" ", " ", " ");
        ArrayList<bookList> toRead = new ArrayList<>();
        bookList example1 = new bookList("Mesopotamia", "Gwendolyn Leick", "history");
        bookList example2 = new bookList("A Week to be Wicked", "Tessa Dare", "romance");
        bookList example3 = new bookList("Say Yes to the Marquess", "Tessa Dare", "romance");
        bookList example4 = new bookList("Istanbul", "Thomas F. Madden", "history");
        bookList example5 = new bookList("Circe", "Madeline Miller", "fiction");
        bookList example6 = new bookList("The Song of Achilles", "Madeline Miller", "fiction");
        bookList example7 = new bookList("A Lady by Midnight", "Tessa Dare", "romance");
        bookList example8 = new bookList("When a Scot Ties the Knot", "Tessa Dare", "romance");
        bookList example9 = new bookList("Worlds of Medieval Europe", "Clifford Backman", "history");
        bookList example10 = new bookList("The Sumerians", "Samuel Noah Kramer", "history");
        Collections.addAll(toRead, example1, example2, example3, example4, example5, example6, example7, example8, example9, example10);

        System.out.println("Welcome to your to read list!");

        System.out.println("To see your reading list, enter 1,");
        System.out.println("To sort your reading list by title, type 2");
        userResponse = scan.nextInt();
        if (userResponse == 1) {
            System.out.println(toRead);
        }
        if (userResponse == 2) {
            Collections.sort(toRead); 
}
}

Collections.sort(toRead);用红色下划线表示“不存在类型变量 T 的实例,因此 bookList 符合 Comparable

我尝试过谷歌和其他问题,但没有帮助。如有任何帮助,我们将不胜感激。谢谢!

(在有人问之前,是的,这是家庭作业。我们应该设计和创建一个程序。我们没有在课堂上讨论对数组列表进行排序)。

java sorting arraylist
2个回答
1
投票

我认为类

bookList
应该命名为
Book
因为该类的实例代表一本书而不是列表。另外,一本书出版后,我认为它的作者和标题(甚至类型)都不会改变,因此它是 [Java] record 的良好候选者。然后你会自动获得方法
toString
以及
equals
hashCode
。由于 [Java] 记录实际上是一个类,因此您可以使其实现 [接口]
Comparable
– 如其他答案 中所示 – 但我想提供替代解决方案,所以我选择实现 Comparator 以及使用 stream API,特别是 sorted 方法。

请注意,以下代码使用 lambda 表达式(和方法引用)。

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public record Book(String title, String author, String genre) {

    public static void main(String[] args) {
        Comparator<Book> bookTitleComparator = (b1, b2) -> b1.title().compareTo(b2.title);
        List<Book> bookList = new ArrayList<>();
        bookList.add(new Book("Mesopotamia", "Gwendolyn Leick", "history"));
        bookList.add(new Book("A Week to be Wicked", "Tessa Dare", "romance"));
        bookList.add(new Book("Say Yes to the Marquess", "Tessa Dare", "romance"));
        bookList.add(new Book("Istanbul", "Thomas F. Madden", "history"));
        bookList.add(new Book("Circe", "Madeline Miller", "fiction"));
        bookList.add(new Book("The Song of Achilles", "Madeline Miller", "fiction"));
        bookList.add(new Book("A Lady by Midnight", "Tessa Dare", "romance"));
        bookList.add(new Book("When a Scot Ties the Knot", "Tessa Dare", "romance"));
        bookList.add(new Book("Worlds of Medieval Europe", "Clifford Backman", "history"));
        bookList.add(new Book("The Sumerians", "Samuel Noah Kramer", "history"));
        bookList.stream()
                .sorted(bookTitleComparator)
                .forEach(System.out::println);
    }
}

运行上面代码时的输出:

Book[title=A Lady by Midnight, author=Tessa Dare, genre=romance]
Book[title=A Week to be Wicked, author=Tessa Dare, genre=romance]
Book[title=Circe, author=Madeline Miller, genre=fiction]
Book[title=Istanbul, author=Thomas F. Madden, genre=history]
Book[title=Mesopotamia, author=Gwendolyn Leick, genre=history]
Book[title=Say Yes to the Marquess, author=Tessa Dare, genre=romance]
Book[title=The Song of Achilles, author=Madeline Miller, genre=fiction]
Book[title=The Sumerians, author=Samuel Noah Kramer, genre=history]
Book[title=When a Scot Ties the Knot, author=Tessa Dare, genre=romance]
Book[title=Worlds of Medieval Europe, author=Clifford Backman, genre=history]

由于 [Java] 记录实际上是一个类,因此您还可以重写任何方法,例如

toString
– 如果您不想要如上所示的默认实现。


0
投票

正如评论中所指出的,您没有为您的程序提供一种对“bookList”进行排序的方法。它应该根据倾斜对其进行排序吗?作者?还有别的东西吗? java 不会仅仅假设“bookList”的自然顺序是什么。

您必须告诉您的程序根据特定条件进行排序,例如标题、作者或类中的任何其他数据字段。

实现此目的的一种方法是让“bookList”类实现 Comparable 接口并重写compareTo() 方法

public class bookList implements Comparable<bookList>{
public String title, author, genre;
public bookList (String beginTitle, String beginAuthor, String beginGenre)
{
    title = beginTitle;
    author = beginAuthor;
    genre = beginGenre;
}
public String getTitle() {
    return title;
}

public String getAuthor() {
    return author;
}

public String getGenre() {
    return genre;
}

public void setTitle(String titleUpdate) {
    title = titleUpdate;
}

public void setAuthor(String authorUpdate) {
    author = authorUpdate;
}

public void setPubDate(String genreUpdate) {
    genre = genreUpdate;
}

@Override
public String toString() {
    return "title: " + title + '\t'+
            "author: " + author + '\t'+
            "genre: " + genre +
             "\n";
}

@Override
public int compareTo(bookList other) {
    //assuming you want to sort by tilte, change it as per your requirements
    return this.title.compareTo(other.title); 
}

}

© www.soinside.com 2019 - 2024. All rights reserved.