简体   繁体   English

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

[英]Adding new objects to an ArrayList using a constructor with an ArrayList

i am doing one of the last ex of the java-programming.mooc.fi PART 7 Recipe search (4 parts).我正在做 java-programming.mooc.fi 第 7 部分食谱搜索(4 个部分)的最后一个例子。 Im almost finished but something its wrong when I initiate the instances for the object Recetas.我快完成了,但是当我为 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:用于测试的 output 是:

Pancake dough, cooking time: 60[tofu, rice, water, carrot, cucumber, avocado, wasabi],煎饼面团,烹饪时间:60[豆腐,米饭,水,胡萝卜,cucumber,牛油果,芥末],

Meatballs, cooking time: 20[tofu, rice, water, carrot, cucumber, avocado, wasabi],丸子,烹调时间:20[豆腐,米饭,水,胡萝卜,cucumber,牛油果,芥末],

Tofu rolls, cooking time: 30[tofu, rice, water, carrot, cucumber, avocado, wasabi]豆腐卷,烹饪时间:30[豆腐,米饭,水,胡萝卜,cucumber,牛油果,芥末]

All tofu ingredients!!所有豆腐成分!

UserInterface.java https://pastebin.com/twvXv04j UserInterface.java https://pastebin.com/twvXv04j

recipes.txt https://pastebin.com/Nwz1RJa7食谱.txt https://pastebin.com/Nwz1RJa7

RecipeAdmin.java https://pastebin.com/1TEYssgS RecipeAdmin.java https://pastebin.com/1TEYssgS

RecipeSearch.java https://pastebin.com/4MbsGeYz RecipeSearch.java https://pastebin.com/4MbsGeYz

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<>();最简单的解决方法是将ingredientes.clear()替换为ingredientes = new ArrayList<>();

i am doing one of the last ex of the java-programming.mooc.fi PART 7 Recipe search (4 parts).我正在做 java-programming.mooc.fi PART 7 Recipe search(4 部分)的最后一个 ex 之一。 Im almost finished but something its wrong when I initiate the instances for the object Recetas.我快完成了,但是当我启动对象 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],煎饼面团,烹饪时间:60[豆腐,米饭,水,胡萝卜,黄瓜,鳄梨,芥末],

Meatballs, cooking time: 20[tofu, rice, water, carrot, cucumber, avocado, wasabi],肉丸子,烹饪时间:20[豆腐、米饭、水、胡萝卜、黄瓜、鳄梨、芥末]、

Tofu rolls, cooking time: 30[tofu, rice, water, carrot, cucumber, avocado, wasabi]豆腐卷,烹饪时间:30【豆腐、米饭、水、胡萝卜、黄瓜、牛油果、芥末】

All tofu ingredients!!所有豆腐原料!!

UserInterface.java https://pastebin.com/twvXv04j UserInterface.java https://pastebin.com/twvXv04j

recipes.txt https://pastebin.com/Nwz1RJa7 recipes.txt https://pastebin.com/Nwz1RJa7

RecipeAdmin.java https://pastebin.com/1TEYssgS RecipeAdmin.java https://pastebin.com/1TEYssgS

RecipeSearch.java https://pastebin.com/4MbsGeYz RecipeSearch.java https://pastebin.com/4MbsGeYz

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.问题是您正在重写相同的 ArrayList。要解决此问题,您需要每次都使用新的 ArrayList object 重新初始化配料参考变量。

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;

 }   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM