英文:
Adding new objects to an ArrayList using a constructor with an ArrayList
问题
以下是您提供的内容的翻译:
我正在完成 Java 编程课程第 7 部分“Recipe Search(4 部分)”的最后一个练习。我几乎完成了,但是在初始化 Recetas 对象的实例时出现了问题。
由于某种原因,每个实例都是正确的,但是“ingredientes”会在所有实例中被新数据覆盖。请在下面找到代码,并提前感谢您的帮助!!
问题出在这里:
public ArrayList<ReceiptAdmin> FileToRecetas(){
ArrayList<String> ingredientes = new ArrayList<>();
int i = 1;
String nombreReceta = "";
int tiempo = 0;
for (String data : fileToArray){
if (data.equals("")){
//System.out.println(ingredientes);
recetas.add(new ReceiptAdmin(nombreReceta, tiempo, ingredientes));
//System.out.println(recetas);
i = 1;
ingredientes.clear();
continue;
}
if (i==1){
nombreReceta = data;
}
if (i==2){
tiempo = Integer.valueOf(data);
}
if (i > 2){
ingredientes.add(data);
}
i++;
}
//System.out.println(ingredientes);
//System.out.println(ingredientes);
this.recetas.add(new ReceiptAdmin(nombreReceta, tiempo, ingredientes));
//System.out.println(recetas);
return recetas;
}
测试输出为:
Pancake dough,烹饪时间:60[豆腐,大米,水,胡萝卜,黄瓜,鳄梨,芥末],
Meatballs,烹饪时间:20[豆腐,大米,水,胡萝卜,黄瓜,鳄梨,芥末],
Tofu rolls,烹饪时间:30[豆腐,大米,水,胡萝卜,黄瓜,鳄梨,芥末]
所有豆腐配料!!
UserInterface.java
https://pastebin.com/twvXv04j
recipes.txt
https://pastebin.com/Nwz1RJa7
RecipeAdmin.java
https://pastebin.com/1TEYssgS
RecipeSearch.java
https://pastebin.com/4MbsGeYz
英文:
i am doing one of the last ex of the java-programming.mooc.fi PART 7 Recipe search (4 parts). Im almost finished but something its wrong when I initiate the instances for the object Recetas.
For some reason each instance is OK but "ingredientes" its overwritten with the new data in all the instances. pls find below the code and thanks in advance for your help!!
The problem its here:
public ArrayList<ReceiptAdmin> FileToRecetas(){
ArrayList <String> ingredientes = new ArrayList<>();
int i = 1;
String nombreReceta = "";
int tiempo = 0;
for (String data : fileToArray){
if (data.equals("")){
//System.out.println(ingredientes);
recetas.add(new ReceiptAdmin(nombreReceta, tiempo, ingredientes));
//System.out.println(recetas);
i = 1;
ingredientes.clear();
continue;
}
if (i==1){
nombreReceta = data;
}
if (i==2){
tiempo = Integer.valueOf(data);
}
if (i > 2){
ingredientes.add(data);
}
i++;
}
//System.out.println(ingredientes);
//System.out.println(ingredientes);
this.recetas.add(new ReceiptAdmin(nombreReceta, tiempo, ingredientes));
//System.out.println(recetas);
return recetas;
}
the output for testing is :
Pancake dough, cooking time: 60[tofu, rice, water, carrot, cucumber, avocado, wasabi],
Meatballs, cooking time: 20[tofu, rice, water, carrot, cucumber, avocado, wasabi],
Tofu rolls, cooking time: 30[tofu, rice, water, carrot, cucumber, avocado, wasabi]
All tofu ingredients!!
UserInterface.java
https://pastebin.com/twvXv04j
recipes.txt
https://pastebin.com/Nwz1RJa7
RecipeAdmin.java
https://pastebin.com/1TEYssgS
RecipeSearch.java
https://pastebin.com/4MbsGeYz
答案1
得分: 3
ArrayList
在每次循环中都需要。不能仅清除列表然后重新开始,必须创建一个新列表。
最简单的修复方法是将ingredientes.clear()
替换为ingredientes = new ArrayList<>();
。
英文:
ArrayList <String> ingredientes = new ArrayList<>();
needs to be in every loop. You can't just clear the list and start over, you have to make a new list.
The simplest fix is to replace ingredientes.clear()
with ingredientes = new ArrayList<>();
答案2
得分: 0
问题在于你一直在覆盖同一个ArrayList。要解决这个问题,你需要每次都使用一个新的ArrayList对象重新初始化"ingredients"引用变量。
将代码重构如下。
if (data.equals("")){
recetas.add(new ReceiptAdmin(nombreReceta, tiempo, ingredientes));
i = 1;
ArrayList<String> ingredientes = new ArrayList<>();//将ingredients.clear()修改为重新使用新的ArrayList<>对象进行初始化。
continue;
}
英文:
Problem is you are over writing the same ArrayList. To solve this issue you need to re initialize ingredients reference variable with a new ArrayList object every time.
Refactor the code like below.
if (data.equals("")){
recetas.add(new ReceiptAdmin(nombreReceta, tiempo, ingredientes));
i = 1;
ArrayList <String> ingredientes = new ArrayList<>();//Change ingredients.clear() to re initializing a new list with new ArrayList<>().
continue;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论