title: How to add an Object to a Generic array using a method Java
body: <p>I want to add an object to my array. I am trying to create a method so whenever it's called, it adds the generic type object. Here's my code:</p>
  
  <pre><code>public class ArrayObjects&lt;E&gt; implements SomeImp&lt;E&gt;{
  
      private E[] list;
  
      private int maxCapacity, currentSize;
  
      public ArrayObjects(){
          maxCapacity = 10;
          array = (E[]) new Object[maxCapacity];
      }
  
      public void addObj(E obj){
          array.add(obj); //Throws an error
      }
  }
  </code></pre>
  
  <p>Eclipse shows me an error though. It says "Cannot invoke add(E) on the array type E[ ]"</p>
  
  <p>Does anyone know why does this happen? Do you know of an alternative of adding an object to my generic array?</p>
  
  <p>Thank you!</p>
  
  <hr>
  
  <p>EDIT:</p>
  
  <p>When I create an instance of a class that instantiates ArrayObjects, and try to add a value to it, it doesn't do it. code:</p>
  
  <pre><code>import packageWhereArrayObjectsIs.*;
  
  
  public class Test {
      private ArrayObjects&lt;Integer&gt; list;
  
      public Test() {
          list = new ArrayObjects&lt;Integer&gt;();    
          Test();
          }
  
      private void TestOne() { 
          for(int i=1; i &lt;= 10; i++)
              list.addLast(i);         
          System.out.println("Should print 1 .. 10");
          System.out.println(list);
  
      }
  }
  </code></pre>