title: Array of generic nodes Java
body: <p>I am implementing a concurrent circular Queue in terms of an array that makes use of separate locks
  at the head and the tail of the queue. Each node in the queue looks as follows:</p>
  
  <pre><code>  private class Node&lt;T&gt; 
    {
          public T item;
          ReentrantLock lock = new ReentrantLock();
          Node(){}
          void lock() {lock.lock();}
          void unlock() {lock.unlock();}
    }
  </code></pre>
  
  <p>I cannot create the queue in the constructor of the queue class.</p>
  
  <pre><code>public Queue(int capacity) {
      items = (Node[]) new Object[capacity];//This line gives the problem
      head = size = 0;
    }
  </code></pre>
  
  <p>I have found a solution <a href="https://stackoverflow.com/questions/13047123/workaround-for-generic-arrays-in-java">here</a>, but this code:</p>
  
  <pre><code>@SuppressWarnings("unchecked")
      Node&lt;T&gt;[] slots = (Node&lt;T&gt;[]) new Node&lt;?&gt;[capacity];
  </code></pre>
  
  <p>Gives the following compiler error:</p>
  
  <pre><code>Cannot create a generic array of Queue&lt;T&gt;.Node&lt;?&gt;
  </code></pre>
  
  <p>My question is what is the correct way to initialize an array of generic objects?</p>