r/programminghelp • u/blubber241 • Oct 25 '21
Java How to add variables once to new array in double for loop
/**
* Returns the resolvents of the two clauses. Take out the terms that negate and new SimpProp is remaining terms
* resolves on the first term
* @param one
* @param two
* @return
*/
public SimpProp resolve(SimpProp one, SimpProp two){
ArrayList<String> oneTerm = one.getTerms();
ArrayList<String> twoTerm = two.getTerms();
ArrayList<String> newTerm = new ArrayList<String>();
for (String i: oneTerm) {
boolean negate = false; //if its true dont add it at the end
for(String j: twoTerm) {
if(!j.contains(i)) {
newTerm.add(j);
}else if(i.equals(j)) {
newTerm.add(j);
}else if(i.contains(j) && !i.equals(j)) {
negate = true;
}
}
if(negate == false) {
newTerm.add(i);
}
}
SimpProp endTerm = new SimpProp(newTerm);
return endTerm;
}So the basis of this method is to look at two phrases that look like B11 V !S21 V B34 and seeing if theres any negation and to return a final array of all the proposition terms that werent negated. So for example (B11 V !S21 V B23)*one* and (!B11 V b32 V S44) *two*, In my code I look at ones terms first and compare it will all terms in two. Im trying to make it so I only add the terms in two once, instead of every iteration of one but im not sure how I would go about it.
4
Upvotes
1
u/skellious Oct 26 '21
Instead of calling newterm.add(j), just add it if negate isn't false. Since you have an if else group, I will never be added unless it doesn't meet the terms to add j.
Sorry for poor formatting I'm on mobile