I need to add lista.get(i)
to an array, it will be added to the array with the value of key spec_linha-criativa
changed. However, in my code I always lose the reference and when j = 1
my list has the same values. For example, when the loop is executed: j = 0 the element 0 of the list is correct, when j = 1, element 1 is correct but element 0 becomes equal to 1 . How to fix this? I already give the new in the object to each interaction to avoid this but it occurs.
private List<BasicDBObject> linhaCriativa(List<BasicDBObject> linhas) {
List<BasicDBObject> lista = new ArrayList<>();
for (int i = 0; i < linhas.size(); i++) {
if (linhas.get(i).toMap().containsKey("spec_linha-criativa")) {
String[] linhaCriativa = linhas.get(i).get("spec_linha-criativa").toString().split(";");
if (linhaCriativa.length > 1) {
for (int j = 0; j < linhaCriativa.length; j++) {
BasicDBObject dbObject = new BasicDBObject();
dbObject = linhas.get(i);
dbObject.replace("spec_linha-criativa", linhaCriativa[j]);
lista.add(dbObject);
}
}
}
}
return lista;
}
I've had the same problem of "losing" reference but I can not apply here link