为什么构造函数数组存储空值除了一个?

时间:2017-05-24 10:56:05

标签: java arrays

我无法弄清楚如何从构造函数中存储对象。到目前为止,我得到的只是一个对象,剩下的都是null。如果有人可以向我解释,以便初学者能够理解,那将非常感激。

        Book catalogue[] = new Book[3];

        int select;
        do
        {
            select = bookMenu();
            switch(select)
            {
            case 1:  
                int i =0;
                if(catalogue[i] != null)
                {
                    JOptionPane.showMessageDialog(null,"Test");
                    break;
                }
                catalogue[i] = addBook();
            case 2:     
                sortBook();     
            break;
            case 3:     
                searchBook(catalogue);   
            break;
            case 4:     
                displayBook(catalogue);  
            break;
            case 5:
            break;
            }   
        }
        while(select != 5);
    }
    public static int bookMenu()
    {
        int select;
        String menuOptions = "--Book store--\n"
                + "\n1. Add book to catalogue"
                + "\n2.Sort and display books by price"
                + "\n3. Search for a book by title" 
                + "\n4. Display all books"
                + "\n\n5. Exit";
        do
        {
            select = Integer.parseInt(JOptionPane.showInputDialog(menuOptions)); 
        }
        while(select < 1 || select > 5);
        return select;  
    }
    public static Book addBook()
    {
        int isbn;
        String title, author;
        Book catalogue = null;
        double price;
        for(int i=0; i<3;i++)
        {   
            isbn = Integer.parseInt(JOptionPane.showInputDialog
                                ("Enter Book ISBN or: "));
            title = JOptionPane.showInputDialog
                                ("Enter Book Title: ");
            author = JOptionPane.showInputDialog
                                ("Enter Book Author: ");
            price = Double.parseDouble(JOptionPane.showInputDialog
                                ("Enter Book Price: "));    
            catalogue = new Book(isbn, title, author, price);    
        }
        return catalogue;
    }

显示方法仅显示最后一个对象,而其他所有对象都为空

public static void displayBook(Book catalogue[])//remain void
{
    String output = "";
    for(Book bk:catalogue)
    {
    output += bk + "\n";
    }        
    JOptionPane.showMessageDialog(null, output);
}

2 个答案:

答案 0 :(得分:1)

case 1:  
    int i =0;
    if(catalogue[i] != null)
    {
        JOptionPane.showMessageDialog(null,"Test");
        break;
    }
    catalogue[i] = addBook();

在每次调用添加图书的过程中,您都会使用i初始化0。因此,新添加的书籍将仅添加到第一个索引上,并被新书覆盖。 因此,全局声明i并在每次成功添加后递增它。

您应该将变量名称更改为某个可读名称,因此我建议您将其更改为numberOfBookscurrentIndex

答案 1 :(得分:0)

我在代码中做了一些更改,现在是

public class Test {
public static void main(String args[]) {

    Book[] catalogue = new Book[3];
    int i = 0;
    int select = 0;
    select = bookMenu();
    do {
        if (i == 3) {
            JOptionPane.showMessageDialog(null, "Books added successfully");
            select = bookMenu();
        }
        switch (select) {
        case 1:
            catalogue[i++] = addBook();
            break;
        case 2:
            sortBook();
            break;
        case 3:
            searchBook(catalogue);
            break;
        case 4:
            displayBook(catalogue);
            break;
        default:
            break;
        }
    } while (select != 5 || i != 3);
}

public static int bookMenu() {
    int select;
    String menuOptions = "--Book store--\n" + "\n1. Add book to catalogue" + "\n2.Sort and display books by price"
            + "\n3. Search for a book by title" + "\n4. Display all books" + "\n\n5. Exit";
    do {
        select = Integer.parseInt(JOptionPane.showInputDialog(menuOptions));
    } while (select < 1 || select > 5);
    return select;
}

public static Book addBook() {
    int isbn;
    String title, author;
    double price;
    isbn = Integer.parseInt(JOptionPane.showInputDialog("Enter Book ISBN or: "));
    title = JOptionPane.showInputDialog("Enter Book Title: ");
    author = JOptionPane.showInputDialog("Enter Book Author: ");
    price = Double.parseDouble(JOptionPane.showInputDialog("Enter Book Price: "));
    return new Book(isbn, title, author, price);
}

public static void sortBook() {

}

public static void searchBook(Book catalogue[])// remain void
{
    String searchValue = JOptionPane.showInputDialog("Enter the title of the book you are searching for");
    boolean found = true;
    for (int i = 0; i < catalogue.length && catalogue[i] != null; i++) {
        if (searchValue.equalsIgnoreCase(catalogue[i].getTitle())) {
            JOptionPane.showMessageDialog(null, "Book details: " + catalogue[i].toString());
            found = true;
        }
    }
    if (found == false) {
        JOptionPane.showMessageDialog(null, "The title does not exist in the collection ");
    }
}

public static void displayBook(Book[] catalogue)// remain void
{
    String output = "";
    for (Book bk : catalogue) {
        output += bk + "\n";
    }
    JOptionPane.showMessageDialog(null, output);
}

}

这是你的Book.java

public class Book {
private int isbn;
private String author;
private String title;
private double price;

/**
 * 
 */
public Book(int isbn, String author, String title, double price) {
    this.isbn = isbn;
    this.author = author;
    this.title = title;
    this.price = price;
}

public String getTitle() {
    return title;
}

@Override
public String toString() {
    return "Book [isbn=" + isbn + ", author=" + author + ", title=" + title + ", price=" + price + "]";
}

}

现在让我解释一下我们在这里缺少的东西。 首先,局部变量“i”每次都被重新初始化,因此将其移到do while循环的一侧。 其次在你的addBook方法中,你在for循环中迭代三次,你期望用户输入但不在任何地方存储这些东西,只有第三次获取的输入才从addBook方法返回Book对象。 另外,我在您的while条件中做了一些更改,以便在运行时代码正常工作。 还可以在Book类中实现toString方法,以便在displayBook方法呈现数组时查看值。