Prevent object from overwriting the rest in ArrayList

-1

I have two repeating loops that have the function of replacing text with other values.

The problem begins when I command to add an object to a ArrayList , in this case the object clone . The addition is in the second loop because the system generates different result for each ingrediente .

Until the line clone.getF().setCalculo(tempA); everything happens normal, but when it goes to line novosItems.add(contaItem, clone); everything starts to go wrong.

The clone object being added replaces all objects contained in ArrayList . Oddly enough with only one value this error occurs. To illustrate what happens, see this example:

//Um arrayList comum 
ArrayList<objeto> lista = new ArrayList<>();
//valores do arrayList lista
[0] = "Maria", "12"
[1] = "João", "10"
[2] = "Carlos", "33"

Now imagine that every time you add something new to ArrayList all values that would be the name, for example, have the same value as the last one entered, but the ages do not change.

lista.add("Roberto")
[0] = "Roberto", "12"
[1] = "Roberto", "10"
[2] = "Roberto", "33"
[3] = "Roberto", "22"

Below is the code that has this problem.

for (Item item : this.tabela.getItem()) {
    // Ingredientes
    final String[] formulainicial = item.getF().getCalculo().clone();
    Item clone = item;

    for (Ingrediente ingrediente : this.tabela.getIngredite()) {

        String tempA[];
        tempA = formulainicial.clone();

        //Subistitue o nome do ingrediente pelo seu valor
        for (int i = 0; i < formulainicial.length; i++) {
            // Procura por nutrientes que contem o mesmo nome e subistitue por valor do nutriente.,
            for (Nutriente n : nuts) {
                if (formulainicial[i].equalsIgnoreCase(n.getNome())) {
                    tempA[i] = String.valueOf(n.getQuantidade());
                }
            }

            if ("MO".equalsIgnoreCase(formulainicial[i])) {
                //Subistitue o texto "MO" pelo valor correspondente.
                tempA[i] = String.valueOf(ingrediente.getQuantidadedeMO())+"";
            }

            if ("total".equalsIgnoreCase(formulainicial[i])) {
                tempA[i] = String.valueOf(totaldeMO);
            }
        }

        listaF.add(contaItem, tempA);
        clone.getF().setCalculo(tempA);
        novosItems.add(contaItem, clone);
        contaItem++;
    }
}
    
asked by anonymous 30.05.2016 / 17:54

1 answer

0

If this is happening there may be two things, or you are passing reference from the same object to the ArrayList or you are passing different but identical objects. I believe it is the second option and the problem is in this line of code:

clone.getF().setCalculo(tempA); .

Now it's hard to say for sure without knowing how the getF() and setCalculo() methods were implemented implemented. Can you post their code?

Another tip I give is to try to break this algorithm into smaller methods that return the value you want, so you're very confused to understand what's going on and make it difficult to maintain and test your code. The more isolated the logical blocks, the more object-oriented your code will be

For example, replace this:

for (int i = 0; i < formulainicial.length; i++) {
        // Procura por nutrientes que contem o mesmo nome e subistitue por valor do nutriente.,
        for (Nutriente n : nuts) {
            if (formulainicial[i].equalsIgnoreCase(n.getNome())) {
                tempA[i] = String.valueOf(n.getQuantidade());
            }
        }

        if ("MO".equalsIgnoreCase(formulainicial[i])) {
            //Subistitue o texto "MO" pelo valor correspondente.
            tempA[i] = String.valueOf(ingrediente.getQuantidadedeMO())+"";
        }

        if ("total".equalsIgnoreCase(formulainicial[i])) {
            tempA[i] = String.valueOf(totaldeMO);
        }
    }

For something more like this:

    // Procura por nutrientes que contem o mesmo nome e subistitue por valor do     nutriente.,
public String substituiNutrientes(String formula){
    for (Nutriente n : nuts) {
                if (formula.equalsIgnoreCase(n.getNome())) {
                    return String.valueOf(n.getQuantidade());
                }
            }
    if ("MO".equalsIgnoreCase(formula)) {
                //Subistitue o texto "MO" pelo valor correspondente.
                return  String.valueOf(ingrediente.getQuantidadedeMO())+"";
            }
    if ("total".equalsIgnoreCase(formula)) {
                return String.valueOf(totaldeMO);
            }    
    return null; 
}


for (int i = 0; i < formulainicial.length; i++) {

    String formulaSubstituida = substituiNutrientes(formula)

    if(formulaSubstituida != null)tempA[i]  = formulaSubstituida;         

}
    
30.05.2016 / 20:38