我有我的代码一个巨大的问题:在Java中另一个ArrayList中添加的ArrayList与迭代
public class BookStore
{
private ArrayList<Book> books;
}
/**
* This method takes the author's name as a String parameter and returns an
* arraylist of all the books written by that author. It uses a while loop
* and an iterator, locates the books written by that author (case-insensitive)
* and adds them to another arraylist.
*/
public ArrayList<Book> getBooksByAuthor(String authorName){
ArrayList<Book> getBooksByAuthor = new ArrayList<Book>();
Iterator<Book> aBook = books.iterator();
while(aBook.hasNext()){
Book aBookd = aBook.next();
if (authorName.equalsIgnoreCase(aBookd.getAuthor())){
books.add(getAuthor());
books.addAll(getBooksByAuthor);
}
}
return getBooksByAuthor.size();
}
那些三线
books.add(getAuthor());
books.addAll(getBooksByAuthor);
和return getBooksByAuthor.size();
我很确定他们是完全错误的。我尝试了不同的方式来做到这一点,但它没有奏效。我真的不明白该怎么做。有人能帮助我吗?感谢您的时间!
你试过运行此代码?它从一个类开始,只定义了一个不可访问的外部数组列表,没有任何方法来操纵该列表,以及一个完全脱离类的函数。代码的其余部分实际调用此函数并执行某些操作的位置在哪里? – 2014-11-25 00:07:04
看起来你只是想'getBooksByAuthor.add(aBookd);'代替你提到的前两行。并且这段代码不会按原样编译 - “getBooksByAuthor.size();'是一个'int',你必须返回一个'ArrayList' - 大概是'getBooksByAuthor'。无关地,你的命名是残酷的。 –
drewmoore
2014-11-25 00:07:13