【问题标题】:ArrayList Book SearchArrayList 图书搜索
【发布时间】:2015-05-26 09:15:26
【问题描述】:

我必须在 bluej 中创建一个图书馆系统,并且它必须能够搜索一本书。但是,我有一个问题。当我尝试搜索一本书时,结果始终是没有可用的书籍...如何排序以使结果显示该书可用?

private List<Book> collection;

public Library()
{
    collection = new ArrayList<Book>();
}

public void addBook(Book book)
{
    collection.add(book);
}

public String titleSearch()
{
    String titleSearch = "\n ";
    for(int i = 0; i < collection.size(); i++){
        if(titleSearch.equalsIgnoreCase(collection.get(i).getTitle())){

            titleSearch = ("\n Book Avaliable");

        }else{
            titleSearch = ("\n No Books Avaliable ");
        }
    }
    return titleSearch;
}

【问题讨论】:

  • 关于编码风格的两个 cmets:使用同一个词作为方法名称和局部变量 ... 是相当糟糕的风格。另外:使用“foreach”循环,例如“for (Book book : collection) {”比“旧式”for循环更易阅读/维护。

标签: java arrays search arraylist bluej


【解决方案1】:

首先,在您的代码中 - 只有最后一本书很重要,如果您找到合适的书,则需要中断,因为不需要检查提醒(并将值重置为“未找到书”)书找到了。

for(int i = 0; i < collection.size(); i++){
    if(titleSearch.equalsIgnoreCase(collection.get(i).getTitle())){
        titleSearch = ("\n Book Avaliable");
        break; //<- added a break here, no need to go on iterating and reset titleSearch later on

    }else{
        titleSearch = ("\n No Books Avaliable ");
    }
}

如果您的收藏为空并且您搜索一本书(显然不存在),上述剧照将失败。
您可以通过避免else来解决它并改进解决方案:

titleSearch = ("\n No Books Avaliable ");
for(int i = 0; i < collection.size(); i++){
    if(titleSearch.equalsIgnoreCase(collection.get(i).getTitle())){
        titleSearch = ("\n Book Avaliable");
        break; //<- added a break here
    }
}

这样你开始悲观 - 书不在那里,如果你以后找到它,你会“改变主意”,并因这个结果而停下来。

上面仍然缺少您实际要查找的标题,这可以通过将其添加为参数并查找来实现:

public String searchTitle(String titleSearch) {
    if (titleSearch == null) return "\n No Books Avaliable ";
    for(int i = 0; i < collection.size(); i++){
        if(titleSearch.equalsIgnoreCase(collection.get(i).getTitle())){
            return "\n Book Avaliable";
        }
    }
    return "\n No Books Avaliable "; //reachable only if no book found
}

最后一点,是使用增强的for each loop

public String searchTitle(String titleSearch) {
    if (titleSearch == null) return "\n No Books Avaliable ";        
    for(Book b : collection){
        if(titleSearch.equalsIgnoreCase(book.getTitle())){
            return "\n Book Avaliable";
        }
    }
    return "\n No Books Avaliable "; //reachable only if no book found
}

【讨论】:

  • 有趣的是,您没有提到他使用错误的变量来检查标题是否匹配。他使用titleSearch 保存他的搜索状态。只有当有一本书包含"\n "(或稍后他的搜索的当前/更新状态)时,此搜索才会成功。你的新方法没有这个错误。
  • @Tom 谢谢,让我也编辑它,出于某种原因,我确信这是参数(首先在方法中缺少)。此外,它仅在第一次迭代中等于“\n” - 在它之后,它是在 else 语句中分配的值
  • 谢谢大家帮了大忙!
【解决方案2】:

如果您使用的是 Java 8,则可以使用流

public String searchTitle(String titleSearch) {

    if(collection.stream().anyMatch(book->{return titleSearch.equalsIgnoreCase(book.getTitle());})){
        return "\n Book Avaliable";
    }
    else{
        return "\n No Books Avaliable";
    }
}

您也可以使用parallelStream() 代替stream()

【讨论】:

    【解决方案3】:

    @amit 我正在使用您的代码。当我使用

    if(titleSearch.equalsIgnoreCase(Book.getTitle()))
    

    代码。我收到一条错误消息,提示“无法从静态上下文中引用非静态方法 'getTitle()'。”

    这是我的图书代码:

    class Book {
    // instance variable
    private String title;
    private String author;
    private String genre;
    private String ISBN;
    private boolean isCheckOut;
    
    
    private static int counter;
    
    Book(String _title) {
        this.title = _title;
        counter++;
    }
    
    /* Get Methods */
    
    public static int getNumOfInstances() {
        return counter;
    }
    
        String getTitle() {
        return this.title;
    }
    
    String getAuthor() {
        return this.author;
    }
    
    String getGenre() {
        return this.genre;
    }
    
    String getISBN() {
        return this.ISBN;
    }
    
    /* All Methods for setting Book */
    
    void setAuthor(String _author) {
        this.author = _author;
    }
    
    void setGenre(String _genre) {
        this.genre = _genre;
    }
    
    void setISBN(String _isbn) {
        this.ISBN = _isbn;
    }
    
    
    
    }
    

    这是我的货架代码:

    import java.util.ArrayList;
    import java.util.List;
    
    public class Shelf {
    
    // instance variable
    private String name;
    private String genre;
    
    private static int counter;
    public ArrayList<Book> books = new ArrayList<Book>();
    
    Shelf(String _name) {
        this.name = _name;
        counter++;
    }
    
    static int getNumOfInstances() {
        return counter;
    }
    
    String getName() {
        return this.name;
    }
    
    String getGenre() {
        return this.genre;
    }
    
    Book getBook(int index) {
        return books.get(index);
    }
    
    int getBookSize() {
        return books.size();
    }
    
    List<Book> getBooks() {
        return this.books;
    }
    
    void setName(String _name) {
        this.name = _name;
    }
    
    void setGenre(String _genre) {
        this.genre = _genre;
    }
    
    void addBook(Book _book) {
        books.add(_book);
    }
    
    
    public String searchTitle(String titleSearch) {
        if (titleSearch == null) return "\n No Books Avaliable ";
        for(Book b : books){
            if(titleSearch.equalsIgnoreCase(Book.getTitle())){
                return "\n Book Avaliable";
            }
        }
        return "\n No Books Avaliable ";
    }
    
    
    }
    

    顺便说一下,布尔值目前没有被使用。

    【讨论】:

      猜你喜欢
      • 2015-01-29
      • 1970-01-01
      • 2012-10-04
      • 1970-01-01
      • 2012-02-01
      • 1970-01-01
      相关资源
      最近更新 更多