title: java.. My array list[] is storing the same value again and again
body: <p>// this code has problem
  public class DirectionList {</p>
  <pre><code>private static Direction[] list;// this array is creating problems
  private int next = 0;
  private final int SIZE = 8;
  public DirectionList()
  {
       list = new Direction[SIZE];// set array size
  }
  
  public void addDirection(Direction d) {
      list[next++] = d;// problematic line
  </code></pre>
  <p>// in this line, the code should input new data in the given index of the array. but the previous data is //erased and new data is overwritten in all index's... suppose I put a in list[0] and then put b in //list[1].. then both list[0] and list[1] becomes b</p>
  <pre><code>    }
  
  }
  
  public String toString()
  {
      String result = &quot;{&quot;;
      
      for(int i = 0; i &lt; list.length; i++)
      {
          if( list[i] == null) {
              result+= &quot;&quot; + i;
          }
          else {
              result += list[i];//list[0].toString();
              
              if(list[i] != null) {
                  
                  result+= &quot;,&quot;;
                  }
              }
      }
      
      
      result+= &quot;}&quot;;
      return result;
  }
  </code></pre>
  <p>}</p>
  <p>//// the code below is another code for testing the output of above code</p>
  <p>public class DirectionListTest {
  public static void main(String[] args) {</p>
  <pre><code>    try {
  
          d.addDirection(new Direction(1, 0));//5
          s = d.toString();
  
          System.out.println(&quot;Test 1 (should be {&lt;down&gt;} ): &quot; + s);
  
          d.addDirection(new Direction(0, -1));
          d.addDirection(new Direction(-1, 1));//6
          s = d.toString();
  
          System.out.println(&quot;Test 2 (should be {&lt;down&gt;,&lt;left&gt;,&lt;up right&gt;} ): &quot; + s);
  
      } catch (NoSuchMethodError e) {
          System.out.println(&quot;Tests failed: Method not named correctly.&quot;);
  
      } catch (Exception e) {
          System.out.println(&quot;Tests failed: &quot; + e.getMessage());
      }
  }
  </code></pre>
  <p>}</p>