I am still learning about encapsulation. I have a GrammarList
, where every Grammar
emcapsulated has an array listRule
with all their setters & getters. as seen here:
public class Grammar {
private enum Type {Left, Right, NULL};
private String Nom;
private static Type type = null;
private static ArrayList<Rule> listRule;
public Grammar(String nom, Type type) {
this.Nom = nom;
this.type = type;
this.listRule = new ArrayList<Rule>();
}
...
}
Now in my program, I notice that my array listRule (where are the rules associated with a grammar are added ) is being overwritten each time a new grammar is added. I have been able to identify that the error happens on the line Grammar grammar = new Grammar(parametre[0], null);
which empties the content of listRule of all other grammars so listRule seems to be the same for every grammar. Is my array listRule created incorrectly or is my loop?
try {
while ((strLine = br.readLine()) != null) {
String[] parametre = strLine.split(",");
Grammar G = GrammarList.containsNom(parametre[0]);
if (G == null) {
Grammar grammar = new Grammar(parametre[0], null);
grammarList.add(grammar);
for (int i = 1; i < parametre.length; i++) {
SyntaxCheck check = new SyntaxCheck(parametre[i]);
if (check.isValid())
grammar.AddRule(check.Rule, check.Sens);
}
}
}
} catch (IOException e1) {
e1.printStackTrace();
}