0
public List<Minterm> completeMinterm(Minterm minterm, String variables){
  List<Minterm> minterms=new ArrayList<Minterm>();

    Minterm m1=new Minterm();
    Minterm m2=new Minterm();
    for (int k = 0; k < minterms.size(); k++) {
        for (int i = 0; i < variables.length(); i++) {
       ..
        }
            m1= minterms.get(k);
            m1.addAtom(new Atom(variables.charAt(i),false));
            m2 = minterms.get(k);
            m2.addAtom(new Atom(variables.charAt(i),true));

        ..
    }
    }

I used eclipse debugger to find errors, I don't understand, why the atom added to m2 is added to m1 too in the same time, when this line is run:

m2.addAtom(new Atom(variables.charAt(i),true));

any Idea why?

Ankur Loriya
  • 3,276
  • 8
  • 31
  • 58
Max_Salah
  • 2,407
  • 11
  • 39
  • 68

1 Answers1

5

After this

m1= minterms.get(k);
m1.addAtom(new Atom(variables.charAt(i),false));
m2 = minterms.get(k);
m2.addAtom(new Atom(variables.charAt(i),true));

m1 and m2 points to the same object (returned from minterms.get(k);). You can try to compare m1 and m2 hash codes or pointers (m1==m2), for example.

gkuzmin
  • 2,414
  • 17
  • 24
  • how can I assign minterms.get(k) to m1 and m2, sothat I can handle m1 undependly from m2 or minterms.get(k)? – Max_Salah Aug 01 '12 at 11:12
  • I've already added comment to your question which says that you can use deep copy algorithms ro reach needed behaviour. – gkuzmin Aug 01 '12 at 11:14
  • @AhmedSalah For further information on deep copies visit: http://stackoverflow.com/questions/5001026/java-deep-copy-library – Baz Aug 01 '12 at 11:25
  • Isn't there an elegant way to solve this problem, for example throw temp varaible? – Max_Salah Aug 01 '12 at 11:25
  • 1
    @AhmedSalah You cannot just use a temp variable, since changing the temp variable also changes the original variable. – Baz Aug 01 '12 at 11:27