title: How to shift elements in array?
body: <p>I am trying to create a method that searches through the 'data' array to find the string 'elt'.  If it exists, it shifts all elements after 'elt' one position to the left (to cover the cell where 'elt' existed).</p>
  
  <p>I am able to find all instances of "elt" and set them to null, but I am having problems shifting all elements past "elt" down one space in the array.  The code below is what I have so far. </p>
  
  <pre><code>public class Bag&lt;T&gt; implements Iterable&lt;T&gt; {
  
  private final int MAXLEN = 3;
  private int size;
  private T[] data; // array
  
  public T remove(T elt) {
  
          for (int i=0; i&lt;data.length; i++) {
              if ("elt".equals(data[i]) ) {
                  data[i] = null;
  
                  for (i++; i &lt; data.length; i++) {
                      data[i] = data[i-1];
                  }
              }
          }
  public static void main(String[] args) {
          Bag&lt;String&gt; sbag = new Bag&lt;String&gt;();
  
          sbag.add("Noriko");
          sbag.add("Buddy");
          sbag.add("Mary");
          sbag.add("Peter");
          sbag.add("elt");
          sbag.add("hello");
  
      Iterator&lt;String&gt; it = sbag.iterator();
          while (it.hasNext()) {
              String val = it.next();
              System.out.println(val);
          }
  
      sbag.remove("elt");
  
      Iterator&lt;String&gt; it2 = sbag.iterator();
          while (it2.hasNext()) {
              String val = it2.next();
              System.out.println(val);
          }
  }
  </code></pre>
  
  <p>When I run that code, I get:</p>
  
  <p>Noriko
  Buddy
  Mary
  Peter
  elt
  hello
  Noriko
  Buddy
  Mary
  Peter
  null</p>
  
  <p>However, I am expecting </p>
  
  <p>Noriko
  Buddy
  Mary
  Peter
  elt
  hello
  Noriko
  Buddy
  Mary
  Peter
  hello</p>
  
  <p>Can anybody tell me how I can fix the code so that the rest of the items in the array are shifted down?  I think the problem is in my remove method.</p>