title: Removing items from a generic array
body: <p>I am trying to remove items from a generic array in Java.</p>
  
  <p>For example an array <code>[1,2,3,4]</code> would become <code>[null,null,null,4]</code> if a 3 was passed in for the <code>n</code> parameter.</p>
  
  <p>My issue is my method is doing nothing to the array. Is there a better way to do this? </p>
  
  <pre><code>import java.util.Arrays;
  import java.util.stream.Collectors;
  
  public class Test&lt;E&gt;
  {
  
  
  
      private E[] queue;
      private int added = 0;
      private int removed = 0;
  
  
      @SuppressWarnings("unchecked")
      public Test(int capacity){
     this.queue=(E[]) new Object[capacity];
  
      }
  
  
      public E[] getQueue(){
          return queue;
      }
  
  
      public void setQueue(E[] items){
          queue = items;
          added=items.length;
          removed = 0;
  
      }
  
  
      public static String formatOutput(Object[] items){
          if (items == null) return "null";
          return String.format("[%s]\n",
                               String.join(", ",
                                           Arrays.stream(items).map(o -&gt; ((o != null) ? o.toString() : "null")).collect(Collectors.toList())));
      }
  
  
      public E[] newArray(int capacity){
  
          @SuppressWarnings("unchecked")
          E[] a= (E[]) new Object[capacity];
          return a;
      }
  
  
      @SuppressWarnings("unchecked")
      public E[] removeNItems(int n){
          E[] a= (E[]) new Object[n];
          if(n&lt;queue.length){
          return null;
         }
       else{
             for(int i=0; i&lt;n;i++){
                 a= (E[]) queue[i];
                 queue[i]=null;
  
                this.removed++;
  
             }}
  
  
             return a;
  
      }
  
  
  
  
  
      public boolean addNItems(E[] items){
          if(items.length+this.added&gt;this.queue.length){
  
  
          return false;
      }
      else{
    for(int i=0; i&lt;items.length;i++){
        queue[i]= (E) items[i];
        added++;
    }
  
  
  
          return true;
      }   
      }
  
  
  
  
      public int size(){
          return added - removed;
      }
  
  
      public static void main(String [] args){
          Test&lt;Integer&gt; test = new Test&lt;&gt;(10);
          System.out.print("Testing the constructor with a capacity of 10: " + formatOutput(test.getQueue()));
          System.out.println("Size of queue after operation: " + test.size());
          // testing the newArray method
          System.out.println("Testing the newArray method with a capacity of 5: " + formatOutput(test.newArray(5)));
  
          System.out.println("Trying to add 5 items");
          Integer[] addFive = {1, 2, 3, 4, 5};
          System.out.println("Able to add 5 items-&gt; " + test.addNItems(addFive));
          System.out.println("Size of queue after operation: " + test.size());
          System.out.println("Array after adding 5 ints: " + formatOutput(test.getQueue()));
  
          System.out.println("Trying to remove 4 items");
          Object[] fourItemsRemoved = test.removeNItems(4);
          System.out.println("Items removed: " + formatOutput(fourItemsRemoved));
          System.out.println("Size of queue after operation: " + test.size());
          System.out.println("Array after trying to remove four items: " + formatOutput(test.getQueue()));
  
  
  
      }
  }
  </code></pre>
  
  <p>Actual Output: </p>
  
  <pre><code>   Trying to remove 4 items
     Items removed: null
     Size of queue after operation: 5
     Array after trying to remove four items: [1, 2, 3, 4, 5, null, null,null, null, null]
  </code></pre>
  
  <p>Expected Output:</p>
  
  <pre><code>Trying to remove 4 items
  Items removed: [1,2,3,4]
  Size of queue after operation:6
  Array after trying to remove four items: [null,null,null,null,5,6]
  </code></pre>