have this class
public class ObjetoOS {
private ArrayList<String> atribute = new ArrayList<String>();
public ObjetoOS (String a){
atribute.add(a);
}
public ObjetoOS (ArrayList<String> a){
atribute = a;
}
which i use like this
public class TablaSimbolica {
private static ArrayList<ObjetoOS> tableOS = null;
public static void addAtributos(ArrayList<String> newOnes){
if (tableOS == null){
tableOS = new ArrayList<ObjetoOS>();
for (String s : newOnes){
ObjetoOS newObj = new ObjetoOS(s);
tableOS.add(newObj);
}
}
else{
ArrayList<ObjetoOS> aux = new ArrayList<ObjetoOS>();
for (ObjetoOS os : tableOS){
ArrayList<String> oldOnes = new ArrayList<String>();
oldOnes = os.getAtributo();
for (String s : newOnes){
oldOnes.add(s);
ObjetoOS newObj = new ObjetoOS(oldOnes);
aux.add(newObj);
newObj = null;
oldOnes.remove(s);
}
}
tableOS = aux;
}
}
so basically: addAtributos checks if the array its empty. if it is it just adds the strings, if its not, i have to combine it with the new ones, like a cartisan product thing.
when adding new strings, although im creating a new array, and a new objectOS which y turn to null after adding it, the elements overwrite with the last one.
for example, if i had strings "true" "false", and i have to add "female" "male", the output is:
[TRUE, Female]
[TRUE, Male]
[TRUE, Male]
[TRUE]
[TRUE]
[FALSE, Female]
[TRUE]
[TRUE]
[FALSE, Male]
[FALSE, Male]
i cant figure out where im missing the point here. it gives me an error if i erase the static both from the ArrayList tablaOS or from the addAtributos method.
EDIT: solved! i changed the object class making a new empty constructor and a method adding a string array with a for.
the loop it ended up being
for (String s : newOnes){
oldOnes.add(s);
ObjetoOS nuevo = new ObjetoOS();
nuevo.addAtribute(oldOnes);
aux.add(nuevo);
oldOnes.remove(s);