title: For loop that shifts array's last value to the left to replace a value x in Java
body: <p>I have the following method that accepts a generic object (When I use it, I pass an <code>Integer</code> object) and removes the object at that position in the array. Note that my </p>
  
  <pre><code>private currentSize;
  private E[] list;
  ...
  public E remove(E obj){
  
      for (int i = 1; i &lt; currentSize; i++)
          if( ((Comparable&lt;E&gt;)obj).compareTo(list[i]) == 0){
  
              for(int j = i; j &lt; currentSize; j++)
                  list[j] = list[j+1];
              currentSize--;
              return obj;
          }
  
      return null;
  }
  </code></pre>
  
  <p>I have two other methods that relate to my method above.</p>
  
  <pre><code>for(int i=1; i &lt;= 10; i++)
      list.addLast(i);
  </code></pre>
  
  <p>The code above adds 10 sequential <code>int</code>s to my array. (This works perfectly. Note that the <code>addLast</code> method parses only a primitive <code>int</code>, because I have it declared like so in <code>addLast()</code> )</p>
  
  <pre><code>for(int i=1; i &lt;= 10; i++){
     list.remove(new Integer(i));  
  }
  </code></pre>
  
  <p>The code above removes those 10 <code>int</code>s we added before. My <code>remove()</code> method kind of works, it removes all <code>Integer</code> objects from the array except 1.</p>
  
  <p>My output when I call both <code>addLast</code> and then <code>remove</code> methods in another class:</p>
  
  <blockquote>
    <p>"1 2 3 4 5 6 7 8 9 10</p>
  </blockquote>
  
  <p>Now removing them all</p>
  
  <blockquote>
    <p>Current size of list: (should be zero) 1"</p>
  </blockquote>
  
  <p>That one in front of (should be zero) is not supposed to be there. Please note that I am trying to ignore the <code>0</code> index of the array. Also, I would like to know the way of implementing a backwards implementation of my inner-most for loop <code>for(int j = i; j &lt; currentSize; j++)</code>. Sort of like <code>[1,2,3,4,5] ...</code> if I want to "remove" #2 in the array, I would start from the end of the array instead of the beginning.</p>
  
  <p>addLast method code:</p>
  
  <pre><code>public void addLast(E obj){
          list[currentSize++] = obj;
          //System.out.println(currentSize);
          }
  </code></pre>
  
  <p>Print all the objects in array:</p>
  
  <pre><code>for(int x : list)
      System.out.print(x + " ");
  System.out.println("\n");
  </code></pre>