简体   繁体   中英

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). 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

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<>();

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

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;

 }   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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