java - 我无法获得列表的总页数
问题描述
到目前为止,如果有人可以帮助我,那是我的代码,我还需要计算总页数,但我无法管理它......
package assignment2;
import java.util.*;
import static java.lang.System.*;
public class Book {
private String title;
//Private class fields
private int numOfPages;
public String getTitle() {
//Getter
return title;
}
public void setTitle(String title) {
//Setter
this.title = title;
}
public int getNumOfPages() {
return numOfPages;
}
public void setNumOfPages(int numOfPages) {
this.numOfPages = numOfPages;
}
public Book(String title, int pages) {
//A parameterized constructor with two parameters
this.setTitle(title);
this.setNumOfPages(numOfPages);
}
public static class Main {
public static void main(String[] args) {
List<Book> books = new LinkedList<>();
books.add(new Book("The Catcher in the Rye", 190));
//List of Book instances
books.add(new Book("Song of Myself", 259));
books.add(new Book("A Man Called Ove", 295));
books.add(new Book("Lolita", 360));
books.add(new Book("The Diary of a Young Girl", 352));
books.add(new Book("The Tattooist of Auschwitz", 288));
books.add(new Book("The Notebook", 214));
List<Book> temporaryBook = new ArrayList<>(books);
//Sublist that stops main method
BookApp bkapp = new BookApp();
int totalPages = bkapp.calcTotalPages(temporaryBook, 0);
out.printf("Total number of books: %d\n", books.size());
out.printf("Total pages: %d\n", totalPages);
}
}
//End of Main class
public static class BookApp {
public int calcTotalPages(List<Book> bookList, int accum) {
if (bookList.size() == 0)
return accum;
return calcTotalPages(bookList, accum + bookList.remove(0).getNumOfPages());
}
}
//End of class BookApp
}
解决方案
你可以这样写calcTotalPages
:
public static int calcTotalPages(List<Book> bookList) {
int sum = 0;
for (Book book : bookList) {
sum += book.getNumOfPages();
}
return sum;
}
或者使用Stream
API 如下:
public static int calcTotalPages(List<Book> bookList) {
return bookList.stream().mapToInt(Book::getNumOfPages).sum();
}
演示:
import java.util.LinkedList;
import java.util.List;
class Book {
private String title;
private int numOfPages;
public Book(String title, int numOfPages) {
this.title = title;
this.numOfPages = numOfPages;
}
public String getTitle() {
return title;
}
public int getNumOfPages() {
return numOfPages;
}
}
public class BookApp {
public static class Main {
public static void main(String[] args) {
List<Book> books = new LinkedList<>();
books.add(new Book("The Catcher in the Rye", 190));
books.add(new Book("Song of Myself", 259));
books.add(new Book("A Man Called Ove", 295));
books.add(new Book("Lolita", 360));
books.add(new Book("The Diary of a Young Girl", 352));
books.add(new Book("The Tattooist of Auschwitz", 288));
books.add(new Book("The Notebook", 214));
int totalPages = calcTotalPages(books);
System.out.printf("Total number of books: %d\n", books.size());
System.out.printf("Total pages: %d\n", totalPages);
}
}
public static int calcTotalPages(List<Book> bookList) {
return bookList.size() == 0 ? 0 : bookList.stream().mapToInt(Book::getNumOfPages).sum();
}
}
输出:
Total number of books: 7
Total pages: 1958
推荐阅读
- java - 试图用空格分割用户输入字符串
- wordpress - WordPress 自定义帖子类型未显示
- java - 弹性搜索:通过 RestHighLevelClient 连接时出现 java.net.ConnectException
- spring - Spring Boot jpa 从 db 中选择数百万条记录并处理数据
- node.js - 在 node.js 中使用 Request v2.88.0 进行摘要式身份验证
- database - 将矩阵导出到 Microsoft Access。使用数据库/插入时出错
- css - 从 CSS 动画的中途平滑过渡到开始(或结束)
- android - Kotlin - 适配器上的“android.app.Application 无法转换为 android.app.Activity”
- angular - 如果 url 参数更改,则激活菜单项
- scala - PartialFunction 隐式参数