在Java中使类数组动态化

时间:2016-01-10 17:56:51

标签: java arrays class

我创建了一个名为' Book'的Java类。使用这个课程,我愿意更新有关新对象&book 39'的信息。我还希望将作者信息添加到对象' book1'中。因此,我使用名为'作者[]'的类数组动态分配内存。我的意思是指我已经创建了一个名为“作者”的类。有自己的一组实例变量。我现在还没有进入。但是,当我在课堂上测试时,我们会对这个课程进行测试。使用另一个名为' TestBook'没有编译错误但是当我运行代码时,我在控制台窗口中收到以下消息:

Exception in thread "main" java.lang.NullPointerException
    at Book.addAuthors(Book.java:34)
    at TestBook.main(TestBook.java:12)

' Book'的代码如下所示:

public class Book {

    private String name;
    private Author[] A = new Author[];
    private int numAuthors = 0;
    private double price;
    private int qtyInStock;

    public Book(String n, Author[] authors, double p) {
        name = n;
        A = authors;
        price = p;
    }
    public Book(String n, Author[] authors, double p, int qIS) {
        name = n;
        A = authors;
        price = p;
        qtyInStock = qIS;
    }
    public Book(String n, double p, int qIS) {
        name = n;
        price = p;
        qtyInStock = qIS;
    }
    public String getName() {
        return name;
    }
    /*
    public Author getAuthors() {
        return A;
    }
    */
    public void addAuthors(Author newAuthor) {
        A[numAuthors] = newAuthor;              // THIS LINE IS WHERE THE ERROR POINTS TO
        ++numAuthors;
    }
    public void printAuthors() {
        /*
        for (int i = 0; i < A.length; i++) {
            System.out.println(A[i]);
        }
        */
        for (int i = 0; i < numAuthors; i++) {
            System.out.println(A[i]);
        }
    }
    public void setPrice(double p) {
        price = p;
    }
    public double getPrice() {
        return price;
    }
    public void setqtyInStock(int qIS) {
        qtyInStock = qIS;
    }
    public int getqtyInStock() {
        return qtyInStock;
    }
    /*
    public String getAuthorName() {
        return A.getName();
    }
    public String getAuthorEmail() {
        return A.getEmail();
    }
    public char getAuthorGender() {
        return A.getGender();
    }
    */
    public String toString() {  
        /*
        return getName() + " " + getAuthor() + " Book price: " + getPrice() + 
        " Qty left in stock: " + getqtyInStock();
        */
        //return getName() + " is written by " + A.length + " authors.";
        return getName() + " is written by " + numAuthors + " authors.";
    }

}

&#39; TestBook&#39;的代码如下所示:

public class TestBook {

    public static void main(String args[]) {
        //Author[] authors = new Author[2];
        //authors[0] = new Author("Tapasvi Dumdam Thapashki",        "tapasvi@thapashki.com", 'M');
        //authors[1] = new Author("Paul Rand", "paulie@aol.com", 'M');
        Book book1 = new Book("The Quickie Man", 69.00, 5);
        //System.out.println(book1.toString());
        //book1.setqtyInStock(5);
        //System.out.println(book1.toString());
        //System.out.println(book1.getAuthorName() + " " + book1.getAuthorEmail());
        //book1.printAuthors();
        book1.addAuthors(new Author("Linda Lee", "lindalee@grinchtown.com", 'F'));
        book1.addAuthors(new Author("Joseph Caputo", "caputo@lfp.com", 'M'));
        System.out.println(book1.toString());
        book1.printAuthors();
    }

}

&#39;作者&#39;的代码如下所示:

public class Author {
    private String name;
    private String email;
    private char gender;

    public Author(String n, String e, char g) {
        name = n;
        email = e;
        gender = g;
    }
    public String getName() {
        return name;
    }
    public void setEmail(String e) {
        email = e;
    }
    public String getEmail() {
        return email;
    }
    public char getGender() {
        return gender;
    }
    public String toString() {  
        return getName() + " [" + getGender() + "] " + getEmail();
    }
}

我想要一些帮助。

2 个答案:

答案 0 :(得分:1)

使用Author

等适当大小初始化private Author[] A = new Author[4];数组

答案 1 :(得分:0)

您忘了指定数组的大小。

private Author[] A = new Author[15];

要制作动态数组,可以使用ArrayList。

private ArrayList<Author> list = new ArrayList<Author>();

addAuthors()

public void addAuthors(Author newAuthor) {

        list.add(newAuthor);
}

printAuthors()

public void printAuthors() {

        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }