Java将项添加到集合中

时间:2016-11-11 02:04:58

标签: java

我需要创建一个方法调用loadBooks。调用时,此方法会创建5本书并将其添加到集合中。

我有什么

public class Library
{

private ArrayList<Book> library;


public Library()
{
   library = new ArrayList<>();
}

 public void loadBooks()
  {
   library.add(new Book("T")); 
   library.add(new Book("A"));
   library.add(new Book("C"))
   library.add(new Book("O"));
   library.add(new Book("S"));
   }

书籍课程:

class Book
 {

private String author;
private String title;
private int numberOfPages;          
private String refNumber;           
private int borrowed;
private int total;
private boolean courseText;


public Book(String bookAuthor, String bookTitle, int numPages,boolean isCourseText)
{
    author = bookAuthor;
    title = bookTitle;
    numberOfPages = numPages;
    refNumber = "";
    courseText = isCourseText;   
}

我得到一个构造函数Book in class Book无法应用于给定的类型错误然后如果我将其更改为其他内容它说找不到类错误。 任何想法做什么或修复我的代码?

1 个答案:

答案 0 :(得分:1)

您的图书类构造函数会收到4个参数,但您只是在loadBooks()方法中传递了一个参数。

只需将loadBooks()中的行更改为:

library.add(new Book("Author", "T", 1205 , true));