我對如何將多個值添加到數組列表中存在一些困惑。將一本書的多個值添加到java中的一個ArrayList中
我試圖做的是將1個對象的多個值添加到數組列表中。
例如:
我有一個數組列表叫做書和IM試圖與它的標題,作者加了一本書,每年出版等等...
這裏就是我有給它一個嘗試,但我不知道如果它的權利或如果有一個更簡單的方法。
public class Book {
public static ArrayList<Book> Books = new ArrayList<>();
public int isbn;
public int yearPublished;
public String title;
public String author;
public String price;
public int noOfCopies;
Book b1 = new Book(1234, 1991, "Book1", "mark.mark", "£11.00", 5);
Book b2 = new Book(4321, 1994, "Book2", "bob.bob", "£12.00", 3);
public Book(int ISBN, int year, String ttle, String auth, String pri, int copies) {
this.isbn = ISBN;
this.yearPublished = year;
this.title = ttle;
this.author = auth;
this.price = pri;
this.noOfCopies = copies;
Books.add(this);
}
}
這是對的嗎?任何人都可以幫我請
你是否得到一個StackOverflowError? –
你應該尊重命名約定。用'Books.add(this);',我們有這樣的印象:add是'Books'類的靜態方法。 – user2336315
刪除** b1 **和** b2 **的定義 –