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++;
}
}