title: Why does adding a new element to an array list replace all the previous elements with the new one?
body: <p>I have a method in my class <code>TextPlan</code>, which calls all my rules in another class <code>RSTRule</code> and adds all rules to an array list. For this method I have used <code>import java.lang.reflect.InvocationTargetException and</code>import java.lang.reflect.Method<code>.
  But it adds to the array list incorrectly. For example, the result of</code>add(RSTRule)` in each call of RST methods are as follows:</p>
  
  <pre><code> call 1: RSTRules.add(RSTRule)=R1
   call 2: RSTRules.add(RSTRule)=R2,R2
   call 3: RSTRules.add(RSTRule)=R3,R3,R3
   call 4: RSTRules.add(RSTRule)=R4,R4,R4
  </code></pre>
  
  <p>That means that every time I add a new element, it removes the previous elements from the array list, but repeats the new element.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>public class TextPlan extends ArrayList&lt;Object&gt;  {
  
    RSTRules rstRules=new RSTRules();
  
    public RSTRules produceAllRSTRules(RSTRules rstRules) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
      RSTRule rstRule=new RSTRule();
          Method[] methods =rstRule.getClass().getMethods();
          for (Method method : methods) {
               if (method.getName().startsWith("generate")) {
              rstRule=(RSTRule)method.invoke(rstRule);
              rstRules.add(rstRule) ;
                 }
           }
            return rstRules;
     }
  }
  </code></pre>
  
  <p>hier one of my methodes in "RSTRule", which i call all of them in "TextPlan" to produce an instance of all Rules;</p>
  
  <pre><code>public class RSTRule{
      protected String ruleName;
      protected DiscourseRelation discourseRelation;
      protected String ruleNucleus;
      protected String ruleSatellite;
      protected String condition;
      int heuristic;
  
  
      public RSTRule generateBothAppearBothCosts_Join(){
              this.discourseRelation=DiscourseRelation.Join;
              this.ruleNucleus="BothProductAppear_Elaboration";
              this.ruleSatellite="BothProductCost_Elaboration";
              this.ruleName="BothProductAppearAndBothProductCost_Join";
              this.condition=null;
              this.heuristic=9;
              return this;
          }
  }
  </code></pre>