按构造函数类中的元素对 ArrayList 进行排序 -Java

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

我创建了一个书单的 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 arraylist constructor
1个回答
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.