Add widget in an Array

Asked

Viewed 1,364 times

1

I need to add lista.get(i) in an array, will be added in 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 the element 1 is correct but the element 0 becomes equal to 1. How to fix it? I already give the new on the object at each interaction to avoid this but 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’t apply here https://stackoverflow.com/questions/29658869/why-cant-i-concatenate-the-json

  • 1

    lista and linha will always be the same size?

  • They are the same things, I brought by parameter twice to try to see if I could change.

1 answer

1


First of all, this place is wrong:

BasicDBObject dbObject = new BasicDBObject();
dbObject = linhas.get(i);

The first assigned value will be lost because soon after another value is assigned without the first being used for anything. Also, as in this part you are iterating the j, but taking with the i, this will always take the same object in the iteration and apply the replace several times in the same object. I think what you want is not to change the BasicDBObject, but create a new BasicDBObject with the amendments.

To simplify your code, I will apply the for-each in its two bonds and in ifs, reverse the condition and use the continue:

private List<BasicDBObject> linhaCriativa(List<BasicDBObject> linhas) {
    List<BasicDBObject> lista = new ArrayList<>();
    for (BasicDBObject db : linhas) {
        if (!db.toMap().containsKey("spec_linha-criativa")) continue;
        String[] linhaCriativa = db.get("spec_linha-criativa").toString().split(";");
        if (linhaCriativa.length == 0) continue;
        for (String criativa : linhaCriativa) {
            BasicDBObject dbObject = new BasicDBObject();
            dbObject = db;
            dbObject.replace("spec_linha-criativa", criativa);
            lista.add(dbObject);
        }
    }
    return lista;
}

Now let’s try to fix the problem:

private List<BasicDBObject> linhaCriativa(List<BasicDBObject> linhas) {
    List<BasicDBObject> lista = new ArrayList<>();
    for (BasicDBObject db : linhas) {
        if (!db.toMap().containsKey("spec_linha-criativa")) continue;
        String[] linhaCriativa = db.get("spec_linha-criativa").toString().split(";");
        if (linhaCriativa.length == 0) continue;
        for (String criativa : linhaCriativa) {
            BasicDBObject dbObject = db.copy();
            dbObject.replace("spec_linha-criativa", criativa);
            lista.add(dbObject);
        }
    }
    return lista;
}

Notice that I invented a method copy(). This method will create a copy of BasicDBObject. This way, I can create multiple copies of each instance and change each of them in a different way without changing the original.

Maybe this still gives some problem that you want to keep a copy unchanged when there is none "spec_linha-criativa" to replace. In this case:

private List<BasicDBObject> linhaCriativa(List<BasicDBObject> linhas) {
    List<BasicDBObject> lista = new ArrayList<>();
    for (BasicDBObject db : linhas) {
        String linhaCriativa = db.toMap().containsKey("spec_linha-criativa")
                ? db.get("spec_linha-criativa").toString().split(";")
                : new String[0];
        if (linhaCriativa.length == 0) {
            lista.add(db.copy());
            continue;
        }
        for (String criativa : linhaCriativa) {
            BasicDBObject dbObject = db.copy();
            dbObject.replace("spec_linha-criativa", criativa);
            lista.add(dbObject);
        }
    }
    return lista;
}
  • Wow, I always have this problem of "losing the object" when I want to replace it. Very enlightening, thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.