在Java中如何通过对象的构造函数将对象添加到数组?

我是一个相当新手的Java开发人员.我有一个用于表示书籍的Book类,以及一个用于表示库的Library类.

Library类中的一些代码:

/**
 * method to populate books list
 */
public static void populateBooksList() {

    // instantiate books

    Book b1 = new Book("Catch 22", "Heller", "Thriller", 5, 1, false);
    Book b2 = new Book("The Hobbit", "Tolkien", "Fantasy", 4, 2, false);
    Book b3 = new Book("Gullivers Travels", "Swift", "Adventure", 3, 3,
            false);
    Book b4 = new Book("Moby Dick", "Melville", "Crime", 4, 4, false);
    Book b5 = new Book("The Lord of the Rings", "Tolkien", "Fantasy", 4, 5,
            false);

    // populate the array

    books[0] = b1;
    books[1] = b2;
    books[2] = b3;
    books[3] = b4;
    books[4] = b5;

}

Book类中的一些代码(我没有粘贴所有的gets / sets):

public class Book {

    /**
     * declaring var for genre
     */
    private String genre;

    /**
     * declaring var for rating
     */
    private int rating;

    /**
     * declaring var for name
     */
    private String name;

    /**
     * declaring var for ID
     */
    private int iD;

    /**
     * declaring var for author
     */
    private String author;

    /**
     * declaring var for onLoan
     */
    private Boolean onLoan;

    /**
     * default constructor
     */
    public Book() {

    }

    /**
     * constructor with args
     * 
     * @param nameP
     *            - book name
     * @param authorP
     *            - book author
     * @param genreP
     *            - book genre
     * @param ratingP
     *            - book rating
     * @param iDP
     *            - book ID
     * @param onLoanP
     *            - onLoan
     */
    public Book(String nameP, String authorP, String genreP, int ratingP,
            int iDP, Boolean onLoanP) {
        this.name = nameP;
        this.iD = iDP;
        this.author = authorP;
        this.onLoan = onLoanP;
        this.genre = genreP;
        this.rating = ratingP;
    }

我想要做的是更有效地将书籍添加到数组中.如果我有50本书,我需要50行代码来填充数组.我想使用Book类中的构造函数,或者以某种方式使用Book类中的gets / sets,或者使用Book类中包含的代码,以便在实例化时自动将每个对象添加到数组中.这可能吗?

谢谢

S.

最佳答案
您可以将书直接分配给数组索引:

books[0] = new Book("Catch 22", "Heller", "Thriller", 5, 1, false);
books[1] = new Book("The Hobbit", "Tolkien", "Fantasy", 4, 2, false);
books[2] = new Book("Gullivers Travels", "Swift", "Adventure", 3, 3, false);
books[3] = new Book("Moby Dick", "Melville", "Crime", 4, 4, false);
books[4] = new Book("The Lord of the Rings", "Tolkien", "Fantasy", 4, 5, false);

编辑回答评论:

在这种情况下,您将直接将书籍分配给数组位置而不使用其他变量.如果你现在做了书b1 =书籍[0];你仍然可以在索引0获得对该书的引用.

事后看来,推荐使用数组初始化程序的其他答案更好,并做同样的事情.这样您甚至不需要预先指定数组或索引的大小:

Book[] books = new Book[] {
    new Book("Catch 22", "Heller", "Thriller", 5, 1, false);
    new Book("The Hobbit", "Tolkien", "Fantasy", 4, 2, false);
    new Book("Gullivers Travels", "Swift", "Adventure", 3, 3, false);
    new Book("Moby Dick", "Melville", "Crime", 4, 4, false);
    new Book("The Lord of the Rings", "Tolkien", "Fantasy", 4, 5, false);
}

转载注明原文:在Java中如何通过对象的构造函数将对象添加到数组? - 代码日志