使用带有 ArrayList 参数的构造函数将新对象添加到 ArrayList 中

huangapple go评论76阅读模式
英文:

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&lt;ReceiptAdmin&gt; FileToRecetas(){
    ArrayList &lt;String&gt; ingredientes = new ArrayList&lt;&gt;();
    int i = 1;
    String nombreReceta = &quot;&quot;;
    int tiempo = 0;
    for (String data : fileToArray){
        if (data.equals(&quot;&quot;)){
            //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 &gt; 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 = new ArrayList<>();
在每次循环中都需要。不能仅清除列表然后重新开始,必须创建一个新列表。

最简单的修复方法是将ingredientes.clear()替换为ingredientes = new ArrayList<>();

英文:
ArrayList &lt;String&gt; ingredientes = new ArrayList&lt;&gt;();

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&lt;&gt;();

答案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(&quot;&quot;)){
         
            recetas.add(new ReceiptAdmin(nombreReceta, tiempo, ingredientes));
            i = 1;
            ArrayList &lt;String&gt; ingredientes = new ArrayList&lt;&gt;();//Change ingredients.clear() to re initializing a new list with new ArrayList&lt;&gt;().
            continue;

 }   

huangapple
  • 本文由 发表于 2020年10月11日 01:34:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/64296371.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定