-1
I have two loops that have the function of replacing texts with other values.
The problem starts when I have an object added to a ArrayList
, in the case the object clone
. The addition is in the second loop because the system generates different result for each ingrediente
.
To the line clone.getF().setCalculo(tempA);
everything happens normal, but when it goes to the line novosItems.add(contaItem, clone);
everything starts to go wrong.
The object clone
which is being added replaces all objects contained in the ArrayList
. Strangely only with a value that occurs this error. 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 become the same value of the last entered, but 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++;
}
}