Add element to an Array

1

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

    
asked by anonymous 03.07.2015 / 20:31

1 answer

1

First, this is wrong:

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

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

To simplify your code, I'm going to apply for-each in its two loops and in if s, invert condition and use 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;
}

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

Maybe this still gives you some trouble that you want to keep a copy unchanged when there is no "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;
}
    
03.07.2015 / 21:23