-1
我正在寫一個BookStore類,它在我現有的書籍類和作者類中使用了一些對象。現在我有一個需要填充的空數組列表。問題是我必須在BookStore類中而不是在測試類中執行此操作。如何將書籍對象添加到此數組列表中?然後它說我應該寫一個方法來獲取書籍。我不確定接下來應該做什麼,而且我已經搜索了很多,但仍然沒有發現類似於我的東西。將圖書對象添加到ArrayList中
import java.util.*;
public class BookStore
{
//we will store Book object references in this
private ArrayList<Book> books;
/*NB: Our reference (books) doesn't refer to anything
**when it is declared so our default constructor
**makes it refer to an empty arraylist*/
public BookStore()
{
books = new ArrayList<Book>();
}
public void addBook(Book bookToAdd)
{
ArrayList<Book> Book1 = new ArrayList<Book>();
if (bookToAdd != null)
{
int noOfBooks = 0;
noOfBooks++; //increasing this counter
}// end if
} // end addBook
}
'books.add(bookToAdd);' – Andrew
您應該使用成員類'books',不在方法內創建新的方法(其範圍僅限於方法)。 然後你只需爲你的類(getters和setter)提供一個mutators。 – Maroun