1

我有以下两个 java 类(如下所列) Class BookInfo 声明静态数组块

     public class BookInfo {

    // Global arrays accessible by all methods

    public static String[] isbnInfo;
    public static String[] bookTitleInfo;
    public static String[] authorInfo;
    public static String[] publisherInfo;
    public static String[] dateAddedInfo;;
    public static int[] qtyOnHandInfo;
    public static double[] wholesaleInfo;
    public static double[] retailInfo;

    static {

        isbnInfo = new String[] {

                                "978-0060014018",
                                "978-0449221431",
                                "978-0545132060",
                                "978-0312474881",
                                "978-0547745527"

                                };

        bookTitleInfo = new String[] {

                                "The Greatest Stories",
                                "The Novel",
                                "Smile",
                                "The Bedford Introduction to Drama",
                                "AWOL on the Appalachian Trail"

                                };

        authorInfo = new String[]  {

                                 "Rick Beyer",
                                 "James A. Michener",
                                 "Raina Telgemeier",
                                 "Lee A. Jacobus",
                                 "David Miller"

                                };

        publisherInfo = new String[] {

                                "HerperResource",
                                "Fawcett",
                                "Graphix",
                                "Bedford St. Martins",
                                "Mariner Books"

                                };

        dateAddedInfo = new String[] {

            "05/18/2003", 
            "07/07/1992", 
            "02/01/2010", 
            "09/05/2008", 
            "11/01/2011"

            };

        qtyOnHandInfo = new int[] {7, 5, 10, 2, 8};

        wholesaleInfo = new double[] {12.91, 7.99, 6.09, 54.99, 10.17};

        retailInfo = new double[] {18.99, 3.84, 4.90, 88.30, 14.95};

    }

    public static void BookInfo() {

        System.out.println("             Serendipity Booksellers");
        System.out.println("                Book Information\n");       


        for(int i = 0; i < isbnInfo.length; i++){

            System.out.println("ISBN: " + isbnInfo[i]);
            System.out.println("Title: " + bookTitleInfo[i]);
            System.out.println("Author: " + authorInfo[i]);
            System.out.println("Publisher: " + publisherInfo[i]);
            System.out.println("Date Added: " + dateAddedInfo[i]);
            System.out.println("Quantity-On-Hand: " + qtyOnHandInfo[i]);
            System.out.println("Wholesale Cost: $ " + wholesaleInfo[i]);
            System.out.println("Retail Price: $ " + retailInfo[i]);
            System.out.println();

        }
    }
    }

如何从此类访问数组列表?到目前为止只有以下内容有效,但是我如何从这个类中修改(添加、删除、编辑等)(这个类中没有主要的主要内容) BookInfo bookinfo = new BookInfo(); bookinfo.BookInfo(); System.out.println(bookinfo.isbnInfo[0]); 如何从主菜单修改(添加、删除、编辑等)

    import java.util.Scanner;

     public class InvMenu {
     public static void addBook(){

      System.out.println("\nYou selected Add a Book\n");
       BookInfo bookinfo = new BookInfo();
      bookinfo.BookInfo(); // only these two are working but I cannot modify arrays at all
      System.out.println(bookinfo.isbnInfo[0]);

        }

       public static void editBook(){

     System.out.println("\nYou selected Edit a Book's Record\n"); 

     }

     public static void deleteBook(){

      System.out.println("\nYou selected Delete a Book\n");

    }

    public static void printInvMenu(){

    String choice;
    int x = 0;
    boolean b;
    char letter;
    boolean menu = true;

    Scanner keyboard = new Scanner(System.in);

    System.out.println("Serendipity Booksellers");
    System.out.println("Inventory Database\n");
    System.out.println("       1. Look Up a Book");
    System.out.println("       2. Add a Book");
    System.out.println("       3. Edit a Book's Record");
    System.out.println("       4. Delete a Book");
    System.out.println("       5. Return to the Main Menu\n");

    do{

    System.out.print("Enter your choice: ");
    choice = keyboard.nextLine();
    b = true;

    try {
        x = Integer.parseInt(choice);
        System.out.println(x);

    }

    catch(NumberFormatException nFE) {

        b = false;
        System.out.println("You did not enter a valid choice. Try again!\n");

    }

       }while(b == false);

    do{

    else if(x == 1){

        addBook();

    }

    else if(x == 2){

        editBook();

    }

    else if(x == 3){

        deleteBook();

    }

    else if(x == 4){

        System.out.println("Returning to the Main Menu\n");
        break;

    }

    else{

        System.out.println("\nYou did not enter a valid choice. Try again!\n");

    }

     printInvMenu();

    }while(x == 5);

      }
     }

我可以从其他类主菜单轻松访问一些功能: BookInfo bookinfo = new BookInfo(); bookinfo.BookInfo(); System.out.println(bookinfo.isbnInfo[0]); 如何从主菜单修改(添加、删除、编辑等)?任何想法,建议都非常感谢!

4

5 回答 5

3

创建数组后,您根本无法将新元素“添加”到数组中。从 oracle 教程页面:

数组是一个容器对象,它包含固定数量的单一类型的值。数组的长度是在创建数组时确定的。创建后,它的长度是固定的。

因此,如果您想在 List 中添加和删除元素,我建议您使用ArrayList对象,它可以定义为

List 接口的可调整大小的数组实现。

例如,您可以替换您的代码行

private static String[] isbnInfo;

为了

private static ArrayList<String> isbnInfo;

并像这样初始化它:

isbnInfo = new ArrayList<String>()
isbnInfo.add("978-0060014018");
isbnInfo.add("978-0449221431");
isbnInfo.add("978-0545132060");
isbnInfo.add("978-0547745527");

至于编辑你的数组,你可以简单地为你的私有字段添加一些公共吸气剂

public static String[] getIsbnInfo()
{
   return isbnInfo;
}

在你的公开课上:

String[] isbnInfo = BookInfo.getIsbnInfo();

您还可以使用公共方法来编辑数组,例如:

public static void replaceIsbnInfo(int index, String isbn)
{
   isbnInfo[index] = isbn;
}

在你的菜单类

BookInfo.replaceIsbnInfo(1, "978-0547745527");

我希望它有所帮助。干杯!

于 2012-10-19T01:01:10.517 回答
1

You've kind of mucked this thing up.

You should first create a "BookInfo" class (but not the one you defined) that contains instance fields of isbnInfo, bookTitleInfo, authorInfo, etc. (Just one entity for one book per field, not an array.)

Then, for each book, create and initialize the corresponding BookInfo object.

Next, either place the collection of such BookInfo objects in a searchable object such as a HashMap (for a single search field), or place them in an array of some sort and build separate HashMaps or whatever to map from search arguments to array index (anchoring all the pieces in a "Library" object).

When someone searches for a book, return the BookInfo object, which can have "getter" methods to extract the (ideally private) instance field values. This returns all info about the book in one piece.

于 2012-10-19T01:18:39.327 回答
0

如果我理解正确,您想从不同的类文件访问数组的值或任何变量/函数吗?

ClassName.VariableName = whatever;

当你使用

ClassName variable = new ClassName();

每次你调用它时它都会运行那个类。确保你的变量是

public static VariableType Variablename;

否则您将无法调用/更改它

于 2012-10-19T00:56:09.627 回答
0

ArrayList 似乎是所有选项中最灵活的。Even Effective Java 第 2 版,第 25 条:更喜欢列表而不是数组。感谢您的输入!

于 2012-10-19T03:47:58.930 回答
0

您已将数组声明为私有,因此它们是私有的。您必须编写+访问器方法+来返回相应的数组,或者更优雅地编写访问、搜索、修改等单个数组条目的方法。

于 2012-10-19T00:53:09.910 回答