title: Iterator design pattern for generic types
body: <p>I am trying to implement Iterator design pattern for generic type. I can not use generic array to store collection, Can someone help me to design Iterator pattern for generic types.</p>
  
  <p>My code here:</p>
  
  <pre><code>public class NamesRepository&lt;T&gt; implements Container&lt;T&gt; {
  
      public NamesRepository(){
          names = new T[]; // Compilation error
      }
      public T names[];
  
      @Override
      public Iterator&lt;T&gt; getIterator() {
          return new NameIterator();
      }
  
      private class NameIterator implements Iterator&lt;T&gt;{
          private int index;
  
          @Override
          public boolean hasNext() {
              if(index&lt;names.length)
                  return true;
              return false;
          }
  
          @Override
          public T next() {
              if(this.hasNext())
                  return names[index++];
              return null;
          }
  
      }
  }
  </code></pre>