我想添加一个默认构造函数到我的数据类型。在默认构造函数下面是问题,“ingredients =”“;”。它给我一个错误说,字符串不能转换为String []。我在等号之后放置什么来编译它?如何初始化字符串数组的默认构造函数?
import java.util.Arrays;
class Recipes {
private String[] ingredients = new String[20];
private String[] instructions = new String[20];
public Recipes(){
ingredients = "" ;
instructions = "" ;
}
public String[] getIngredients() {
return ingredients;
}
public void setIngredients(String[] inIngredients) {
ingredients = inIngredients;
}
public String[] getInstructions() {
return instructions;
}
public void setInstructions(String[] inInstructions) {
instructions = inInstructions;
}
public void displayAll() {
System.out.println("The ingredients are " + ingredients);
System.out.println("The instructions are " + instructions);
}
}
谢谢你的回应。我的老师说他希望我们初始化两个数组,以便每个数组中的每个索引都包含空字符串。我是相当新的,不知道将它们分配给null是否会满足他的要求。 – James
然后,您需要第二个选项,而不是第一个或第三个选项。 – rgettman
'Arrays.fill'也可以,而不是for循环。 –