我试图查看我的书籍清单并返回该特定作者的所有书籍。我回来了一个空列表。
public ArrayList<Book> searchByAuthor(Author author) {
ArrayList<Book> bookList = new ArrayList<>();
for (int i = 0; i < myBooks.size(); i++) {
if (myBooks.get(i).getMyAuthors().contains(author)) {
bookList.add(myBooks.get(i));
}
}
return bookList;
}
public ArrayList<Author> getMyAuthors() {
return myAuthors;
}
主要看起来像这样的事情。
Author author = new Author("James");
System.out.println(bookCollections.searchByAuthor(author));
答案 0 :(得分:0)
您的作者对象是一个值对象。您需要在Author.java类中实现equals
和hashcode
方法。