Java图书馆项目中ArrayList无法正确存储Book对象该如何解决?
问题原因及修正方案
核心错误点
- Book类的
title属性误用static修饰:static属性属于类本身,所有Book实例共享同一个title值,每次新建Book对象都会覆盖全局title,导致所有书的标题都变成最后一本的标题 - 字符串相等判断错误:使用
==比较字符串只会对比内存地址,不会对比内容,要改用equals()方法 - Library类定义了多余的全局成员
private Book book和private String title,借书/还书逻辑中错误使用这个全局book变量,而非遍历过程中匹配到的当前Book实例 - borrowBook遍历逻辑不完善:匹配到对应书籍后没有跳出循环,遍历其他不匹配的书籍时会重复输出错误提示
printAvailableBooks直接打印ArrayList会输出对象内存地址,需要遍历输出未借出的书籍标题- 导入语句错误:
import package.Book;为无效语法,同包下无需导入Book类
修正后的代码
Book类
public class Book { private boolean borrowed; // 去掉static修饰,改为实例属性,每个Book有独立标题 private String title; // 构造方法 public Book(String title) { this.title = title; borrowed = false; } // 标记为已借出 public void borrowed() { borrowed = true; } // 标记为已归还 public void returned() { borrowed = false; } public boolean isBorrowed() { return borrowed; } // 去掉static修饰,改为实例方法 public String getTitle() { return title; } }
Library类
import java.util.ArrayList; // 同包下无需导入Book类,删掉错误的import package.Book; public class Library { private String hours; private String address; // 删掉多余的全局成员 private String title; 和 private Book book; private ArrayList<Book> books = new ArrayList<Book>(); // 构造方法 public Library(String address) { this.address = address; hours = "9 am to 5 pm"; } void printOpeningHours() { System.out.println(hours); } void printAddress() { System.out.println(address); } void addBook(Book book) { books.add(book); } void printAvailableBooks() { if (books.size() == 0){ System.out.println("There are no books available"); return; } // 遍历输出未借出的书籍 for (Book book : books) { if (!book.isBorrowed()) { System.out.println(book.getTitle()); } } } void borrowBook(String title) { boolean bookFound = false; for (Book currentBook : books) { // 用equals对比字符串内容 if (currentBook.getTitle().equals(title)) { bookFound = true; if (!currentBook.isBorrowed()){ currentBook.borrowed(); System.out.println("Successfully borrowed " + title); } else { System.out.println("Sorry, that book is already checked out"); } // 匹配到书就跳出循环 break; } } if (!bookFound) { System.out.println("Sorry, no such book is available"); } } void returnBook(String title) { for (Book currentBook : books) { if (currentBook.getTitle().equals(title)) { if (currentBook.isBorrowed()){ currentBook.returned(); System.out.println("Successfully returned " + title); } break; } } } public static void main(String[] args) { // Create two libraries Library firstLibrary = new Library("10 Main St."); Library secondLibrary = new Library("228 Liberty St."); // Add four books to the first library firstLibrary.addBook(new Book("The Da Vinci Code")); firstLibrary.addBook(new Book("Le Petit Prince")); firstLibrary.addBook(new Book("A Tale of Two Cities")); firstLibrary.addBook(new Book("The Lord of the Rings")); // Print opening hours and the addresses System.out.println("Library hours:"); firstLibrary.printOpeningHours(); System.out.println("Library addresses:"); firstLibrary.printAddress(); System.out.println(); System.out.println("Library hours:"); secondLibrary.printOpeningHours(); System.out.println("Library addresses:"); secondLibrary.printAddress(); System.out.println(); // Try to borrow The Lord of the Rings from both libraries System.out.println("Borrowing The Lord of the Rings:"); firstLibrary.borrowBook("The Lord of the Rings"); secondLibrary.borrowBook("The Lord of the Rings"); System.out.println(); // Print the titles of all available books from both libraries System.out.println("Books available in the first library:"); firstLibrary.printAvailableBooks(); System.out.println(); System.out.println("Books available in the second library:"); secondLibrary.printAvailableBooks(); System.out.println(); // Return The Lord of the Rings to the first library System.out.println("Returning The Lord of the Rings:"); firstLibrary.returnBook("The Lord of the Rings"); System.out.println(); System.out.println("Books available in the first library after return:"); firstLibrary.printAvailableBooks(); } }
内容的提问来源于stack exchange,提问作者yungstirjoey666
相关产品推荐
相关产品推荐

