【问题标题】:Making a class-array dynamic in Java在 Java 中使类数组动态化
【发布时间】:2016-01-10 17:56:51
【问题描述】:

我创建了一个名为“Book”的 Java 类。使用此类,我愿意更新有关新对象“book1”的信息。我还想将作者信息添加到对象“book1”中。因此,我使用名为“Author[]”的类数组动态分配内存。我的意思是有一个单独的代码,我在其中创建了一个名为“Author”的类,它有自己的一组实例变量。我现在不参与。但是,当我使用另一个名为“TestBook”的类测试“Book”类时,没有编译错误,但是当我运行代码时,我在控制台窗口中收到以下消息:

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

“书”的代码如下所示:

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.";
    }

}

'TestBook'的代码如下所示:

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();
    }

}

“作者”的代码如下所示:

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();
    }
}

我需要一些帮助。

【问题讨论】:

  • private Author[] A = new Author[]; 是什么意思????
  • 我建议您将Author[] A 替换为List&lt;Author&gt; authors,这样您就无需调整大小或跟踪作者的数量。

标签: java arrays class


【解决方案1】:

用适当的大小初始化Author数组,如private Author[] A = new Author[4];

【讨论】:

    【解决方案2】:

    您忘记指定数组的大小。

    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));
            }
        }
    

    【讨论】:

    • 嗨!根据您的建议,我对“Book”类进行了更改。但是,我仍然收到错误消息 - 找不到符号,符号:类 ArrayList。我已将前两个构造函数(名为“Book”)中的第二个参数从“Author[] authors”更改为“ArrayList authors”,并且在构造函数中分配“authors”时还进行了其他必要的更改即从“A = authors”到“list = authors”。我希望你能明白我想说的。
    • 我想,你忘了导入 ArrayList 类,import java.util.ArrayList;
    • 感谢您抽出宝贵时间帮助我。它现在正在工作。
    猜你喜欢
    • 1970-01-01
    • 2013-04-11
    • 2019-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多