title: Removing a element from an array list without using libraries. Java
body: <p>I have implemented a function which removes an element from an array list. I should not the ArrayList libraries! See my code below:</p>
  
  <pre><code>/**
   * removes a LendItem at a specified (index) position.
   * This functions returns the item removed from the list or null if no such item exists. This
   * function leaves no gaps, that means all items after the removed item are shifted one position.
   * @param list is the item to be removed
   * @param n is the index of the item to be removed
   * @return the removed item
   */
  
  public static LendItem remove(LendItemArrayList list, int n) {
  
      if (list.next == 0) {
          return null;
      }
  
      if (n &lt; 0 || n &gt; list.INITIAL_SIZE) {
          return null;
      }
  
      LendItem itemToBeRemoved = list.lendItems[n]; // itemToBeRemoved is the item which has the index n, which we want to remove from the list.
      int i;
      for (i = n; i &lt; list.next - 1; i++) { // iterate through the list, starting where the index of the itemToBeRemoved is. 
          list.lendItems[i] = list.lendItems[i + 1];
      }
  
      list.lendItems[i] = null;
      list.next--;
      return itemToBeRemoved;
  }
  </code></pre>
  
  <p>and here is the class : </p>
  
  <pre><code>public class LendItemArrayList {
  
      int INITIAL_SIZE = 20;
      boolean resizeable = false;
      LendItem[] lendItems = new LendItem[INITIAL_SIZE];
      int next = 0;
  }
  </code></pre>
  
  <p>I have tested my functions with a few test methods which have been provided, and i am only failing one of them. Specifically the test is called: </p>
  
  <pre><code>removeNonExistingElement
  </code></pre>
  
  <p>and it fails like this:</p>
  
  <blockquote>
    <p>java.lang.AssertionError: 10 elements have been added, next should be 10 (no changes) but found 9.</p>
  </blockquote>
  
  <p><strong>EDIT:</strong> 
  Added the add() function.</p>
  
  <pre><code>public static boolean add(LendItemArrayList list, LendItem p) {
  
      if (list.next == list.lendItems.length) {
          if (list.resizeable == false) {
              return false;
          }
      }
  
      if (list.next == list.lendItems.length) {
          if (list.resizeable == true) {
  
              LendItem[] resizedList = new LendItem[list.lendItems.length*2]; 
  
              for (int i = 0; i &lt; list.next; i++) {                           
                  resizedList[i] = list.lendItems[i];                         
              }
  
              list.lendItems = resizedList;
  
          }
      }
  
      list.lendItems[list.next++] = p;                                        
      return true;
  }
  </code></pre>