Java 构造函数中的字符串[]或ArrayList?
我必须为ArrayList创建一个构造函数。所以基本上我想要的是这个构造函数生成一个ArrayListJava 构造函数中的字符串[]或ArrayList?,java,arrays,arraylist,Java,Arrays,Arraylist,我必须为ArrayList创建一个构造函数。所以基本上我想要的是这个构造函数生成一个ArrayList public Recipe(String[] modifications){ The problem is, it should give something like(example): String[] modifications = [heatup, cooldown, mix, heatup] 建造师是否会这样工作: Recipe(heatup, cooldown
public Recipe(String[] modifications){
The problem is, it should give something like(example):
String[] modifications = [heatup, cooldown, mix, heatup]
建造师是否会这样工作:
Recipe(heatup, cooldown, mix, heatup)
private final List<String> list;
public Recipe(String ... modifications){
this.list = new ArrayList<String>(Arrays.asList(modifications));
}
Recipe(heatup, cooldown, mix, heatup);
或者我需要把它改成这样的东西:
Recipe(modifications){
this.myArrayList[1] = modifications[1]
.
.
.
}
如果我的代码中有一些大错误,仍然在学习我的Java代码,请向您表示感谢和歉意。您可以这样定义它:
public Recipe(String... modifications)
这将为您提供一个可变参数列表,因此您的构造函数的工作方式如下:Recipe(加热、冷却、混合)
在构造函数内部,您可以像使用数组一样使用它。您可以这样定义它:
public Recipe(String... modifications)
这将为您提供一个可变参数列表,因此您的构造函数的工作方式如下:Recipe(加热、冷却、混合)
在构造函数内部,您可以像使用数组一样使用它。将构造函数更改为varargs:
public Recipe(String ... modifications){
您可以通过String[]
访问modifications
,但是,这是一个数组,而不是一个ArrayList。如果需要ArrayList,请执行以下操作:
Recipe(heatup, cooldown, mix, heatup)
private final List<String> list;
public Recipe(String ... modifications){
this.list = new ArrayList<String>(Arrays.asList(modifications));
}
Recipe(heatup, cooldown, mix, heatup);
私人最终名单;
公共配方(字符串…修改){
this.list=newarraylist(Arrays.asList(modifications));
}
将构造函数更改为varargs:
public Recipe(String ... modifications){
您可以通过String[]
访问modifications
,但是,这是一个数组,而不是一个ArrayList。如果需要ArrayList,请执行以下操作:
Recipe(heatup, cooldown, mix, heatup)
private final List<String> list;
public Recipe(String ... modifications){
this.list = new ArrayList<String>(Arrays.asList(modifications));
}
Recipe(heatup, cooldown, mix, heatup);
私人最终名单;
公共配方(字符串…修改){
this.list=newarraylist(Arrays.asList(modifications));
}
您可以使用varargs参数创建构造函数:
public Recipe(String... modifications){...}
然后你可以这样称呼它:
Recipe(heatup, cooldown, mix, heatup)
private final List<String> list;
public Recipe(String ... modifications){
this.list = new ArrayList<String>(Arrays.asList(modifications));
}
Recipe(heatup, cooldown, mix, heatup);
但请注意,如果您(计划)有其他具有潜在冲突参数列表的构造函数,则应小心使用varargs参数。但是,如果这是您唯一的构造函数,那么varargs应该可以使用。您可以使用varargs参数创建一个构造函数:
public Recipe(String... modifications){...}
然后你可以这样称呼它:
Recipe(heatup, cooldown, mix, heatup)
private final List<String> list;
public Recipe(String ... modifications){
this.list = new ArrayList<String>(Arrays.asList(modifications));
}
Recipe(heatup, cooldown, mix, heatup);
但请注意,如果您(计划)有其他具有潜在冲突参数列表的构造函数,则应小心使用varargs参数。但是,如果这是您唯一的构造函数,那么varargs应该可以使用。您可以在构造函数中使用它
ArrayList<String> list=new ArrayList<String>();
public Recipe(String[] modifications){
// String a[]={"asas"};
list.addAll(Arrays.asList(modifications));
ArrayList list=new ArrayList();
公共配方(字符串[]修改){
//字符串a[]={“asas”};
addAll(Arrays.asList(modifications));
您可以在构造函数中使用此选项
ArrayList<String> list=new ArrayList<String>();
public Recipe(String[] modifications){
// String a[]={"asas"};
list.addAll(Arrays.asList(modifications));
ArrayList list=new ArrayList();
公共配方(字符串[]修改){
//字符串a[]={“asas”};
addAll(Arrays.asList(modifications));
第二个……当然,如果您只想复制元素,可以使用for
此外,您还可以将参数传递的数组复制到类属性/方法变量中,除非另有不这样做的动机
最后,请记住它类似于Recipy(String[]modifications){
最后,myArrayList对于数组来说是一个有点不幸的名称(API中有一个名为ArrayList的类)。第二个名称……如果您只想复制元素,当然可以使用
for
此外,您还可以将参数传递的数组复制到类属性/方法变量中,除非另有不这样做的动机
最后,请记住它类似于Recipy(String[]modifications){
最后,myArrayList是数组的一个不太好的名称(API中有一个名为ArrayList的类)。您想要的是能够传递数量不确定的参数:
public Recipe(String ... modifications) {
在构造函数中,修改是一个数组
这将适用于以下情况:
new Recipe("heatup", "cooldown", "mix", "heatup");
new Recipe();
new Recipe("heatup", "mix");
因此:
new Recipe("heatup", "cooldown", "mix", "heatup");
new Recipe();
new Recipe("heatup", "mix");
因此:
new Recipe("heatup", "cooldown", "mix", "heatup");
new Recipe();
new Recipe("heatup", "mix");
您想要的是能够传递数量不确定的参数:
public Recipe(String ... modifications) {
在构造函数中,修改是一个数组
这将适用于以下情况:
new Recipe("heatup", "cooldown", "mix", "heatup");
new Recipe();
new Recipe("heatup", "mix");
因此:
new Recipe("heatup", "cooldown", "mix", "heatup");
new Recipe();
new Recipe("heatup", "mix");
因此:
new Recipe("heatup", "cooldown", "mix", "heatup");
new Recipe();
new Recipe("heatup", "mix");
您可以使用
varargs
:
public Recipe(String ... modifications) {
// now you can use modifications as an array
for (int f = 0; f < modifications.length; f++) {
System.out.println("#" + f + ": " + modifications[f]);
}
}
.您可以使用
varargs
:
public Recipe(String ... modifications) {
// now you can use modifications as an array
for (int f = 0; f < modifications.length; f++) {
System.out.println("#" + f + ": " + modifications[f]);
}
}
.哦,伙计,我觉得自己很傻:p我知道数组和ArrayList之间的区别,但是,我现在太深入了,我一直在想事情:)我真的很感谢这里的每个人:)!哦,伙计,我觉得自己很傻:p我知道数组和ArrayList之间的区别,但是,我现在太深入了,我一直在想事情:)我真的很感谢在座的每一个人:)!