为了将代码保存在主空间中,我在另一个类方法中编写了一些业余代码,我希望将其实现到我的main方法中。但是,每当为该类创建对象后尝试调用该方法时,都会收到一条错误消息。你能开导我并告诉我我做错了什么吗?
这是我在数组类中编写的代码。
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()
答案 0 :(得分:2)
如果类this.dialog.open(MyDialogComponent, {maxWidth: '100vw !important'});
具有一个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);
}
}
}
答案 1 :(得分:0)
您没有将任何参数传递给您的方法调用。如果要使用班级中的列表,请更改为此方法
public void printList() {
for(String x:bookList) {
System.out.println(x);
}