On this assignment, I've been given a list of books, but I need to trim down the list of books so only the books that are equal to or less than the max number of pages remain. However, when I try running the code, some of the books are removed properly, but the ones that are above the maximum 400 or 500 pages are not. How can I fix this issue?
Here is the code for the method for the assignment:
public List<Book> filterBooks(List<Book> readingList, int maxPages)
{
List<Book> correctPages = new ArrayList<Book>();
for(int i = 0; i < readingList.size(); i++)
{
if(readingList.get(i).getNumPages() <= maxPages)
{
Book bookMaxPages = readingList.remove(i);
correctPages.add(bookMaxPages);
}
}
return correctPages;
}