我正在尝试迭代数组列表并根据特定条件将新书籍(自定义类)添加到列表中。我的 addBook 函数如下。 bookIter.add() 行给了我错误:“找不到符号方法 add(Book)”。任何帮助将不胜感激。
谢谢
public void addBook(String title, String authors, String publisher, int year) {
Iterator<Book> bookIter;
bookIter = this.books.iterator();
if (this.books.isEmpty()) {
this.books.add(new Book(title, authors, publisher, year));
System.out.println(this.books.toString());
} else {
while (bookIter.hasNext()) {
Book book = bookIter.next();
if (book.getYear() == year) {
System.out.println("Duplicate Years. Please try again.");
} else {
bookIter.add(new Book(title, authors, publisher, year));
System.out.println(this.books.toString());
}
}
}
}
最佳答案
Iterator
不包含 add()
方法,ListIterator
包含。
尝试将其更改为:
ListIterator<Book> bookIter;
bookIter = this.books.listIterator();
关于java - 迭代器找不到符号方法 add(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33190004/