Java 从main中的另一个类方法调用数组

Java 从main中的另一个类方法调用数组,java,arrays,methods,Java,Arrays,Methods,为了在主空间中保存代码,我在另一个类方法中编写了一些业余代码,我想将其实现到我的主方法中。但是,每当我在为该类创建对象后尝试调用该方法时,都会收到一条错误消息。你能告诉我我做错了什么吗 这是我在array类中编写的代码 public ListBook() { String[]bookList= new String[11]; bookList[0]="Necromonicon"; bookList[1]="The Hobbit"; bookList[2]="Han

为了在主空间中保存代码,我在另一个类方法中编写了一些业余代码,我想将其实现到我的主方法中。但是,每当我在为该类创建对象后尝试调用该方法时,都会收到一条错误消息。你能告诉我我做错了什么吗

这是我在array类中编写的代码

public ListBook() {
    String[]bookList= new String[11];
    bookList[0]="Necromonicon";
    bookList[1]="The Hobbit";
    bookList[2]="Hannibal";
    bookList[3]="Cooking an egg";
    bookList[4]="The Hulk smashes again";
    bookList[5]="The Tyranny of a king";
    bookList[6]="The Phantom Menace";
    bookList[7]="Rogue One: A Starwars Story";
    bookList[8]="The Mighty Hercules";
    bookList[9]="The Serpents Gaze";
    bookList[10]="The End of the World";    
    }


public void printList(String bookList[]) {
    for(String x:bookList) {
    System.out.println(x);
}
这是主屏幕上的代码:

public static void main(String[] args) {
    ListBook r = new ListBook();
    r.printList();
}
错误消息:

The method printList(String[]) in the type ListBook is not applicable for the arguments()

您没有向方法调用传递任何参数。如果希望在类中使用列表,请将方法更改为此

public void printList() {
    for(String x:bookList) {
    System.out.println(x);
}

您没有向方法调用传递任何参数。如果希望在类中使用列表,请将方法更改为此

public void printList() {
    for(String x:bookList) {
    System.out.println(x);
}

如果类
ListBook
有一个
String
数组,那么当调用
printList()
时,您将读取该数组。因为现在的问题是数组与实例相关,不应作为参数传递

public class ListBook {

    private static String[] defaultBooks = {"Necromonicon", "The Hobbit", "Hannibal", "Cooking an egg", "The Hulk smashes again", "The Tyranny of a king",
            "The Phantom Menace", "Rogue One: A Starwars Story", "The Mighty Hercules", "The Serpents Gaze", "The End of the World"};

    private String[] bookList;

    public ListBook() {
        this(defaultBooks);
    }

    public ListBook(String[] books) {
        bookList = books;
    }

    public void printList() {
        for (String x : bookList) {
            System.out.println(x);
        }
    }
}
并用作

public static void main(String[] args) {
    ListBook r = new ListBook();
    r.printList();
    // OR
    ListBook r2 = new ListBook(new String[]{"Book 1", "Book 2", "Book 3"});
    r2.printList();
}

如果类
ListBook
有一个
String
数组,那么当调用
printList()
时,您将读取该数组。因为现在的问题是数组与实例相关,不应作为参数传递

public class ListBook {

    private static String[] defaultBooks = {"Necromonicon", "The Hobbit", "Hannibal", "Cooking an egg", "The Hulk smashes again", "The Tyranny of a king",
            "The Phantom Menace", "Rogue One: A Starwars Story", "The Mighty Hercules", "The Serpents Gaze", "The End of the World"};

    private String[] bookList;

    public ListBook() {
        this(defaultBooks);
    }

    public ListBook(String[] books) {
        bookList = books;
    }

    public void printList() {
        for (String x : bookList) {
            System.out.println(x);
        }
    }
}
并用作

public static void main(String[] args) {
    ListBook r = new ListBook();
    r.printList();
    // OR
    ListBook r2 = new ListBook(new String[]{"Book 1", "Book 2", "Book 3"});
    r2.printList();
}

构造函数通常初始化一个您重用的字段,因此您不必将其作为参数传递。构造函数通常初始化一个您重用的字段,因此您不必将其作为参数传递。谢谢!向我解释了很多,还有新的编码格式。谢谢!向我解释了很多,还有新的编码格式。