title: How to remove element from private ArrayList in generic class
body: <p>I have generic class MyArray where private member is ArrayList, and inside is implemented iterator. </p>
  
  <p>In Main is given some MyArray with strings and I want to delete all "test" from it... Problem is in iterator which method remove doesn't work</p>
  
  <p>Here is how class looks like:</p>
  
  <pre><code>public class MyArray&lt;E&gt; {
          private ArrayList&lt;E&gt; list;
  
          public MyArray() {
              list = new ArrayList&lt;E&gt;();
          }
  
          public int length() {    return list.size(); }
  
          public E at(int pos) {    return list.get(pos); }
  
          public void add(E val) {    list.add(val); }
  
          public void remove(int pos) {    list.remove(pos); }
  
          public class MyIterator implements Iterator&lt;E&gt;{
              int index;
  
              @Override
              public boolean hasNext() {
                  return index &lt; list.size();
              }
  
              @Override
              public E next() {
                  if (!hasNext())
                      throw new NoSuchElementException("no next value");
                  E tmp = list.get(index);
                  index++;
                  return tmp;
              }
  
          }
  
          public Iterator&lt;E&gt; iterator() {
              return new MyIterator();
          }
  
      public static void main(String[] args) {        
          MyArray&lt;String&gt; b = new MyArray&lt;String&gt;();
          b.add("This");
          b.add("is");
          b.add("test");
          b.add("please");
          b.add("delete");
          b.add("all");
          b.add("test");
  
          Iterator&lt;String&gt; iter = b.iterator();
          while(iter.hasNext())
              System.out.println(iter.next());
  
          for(Iterator&lt;String&gt; i = b.iterator(); i.hasNext(); ) {
              String tmp = i.next();
              if (tmp.equals("test"))
                  i.remove();
          }
  
          Iterator&lt;String&gt; ite = b.iterator();
          while(ite.hasNext())
              System.out.println(ite.next());
      }
  
  }
  </code></pre>
  
  <p>Exception that I get is:</p>
  
  <pre><code>Exception in thread "main" java.lang.UnsupportedOperationException: remove
      at java.util.Iterator.remove(Unknown Source)
      at cas1.MyArray.main(MyArray.java:71)
  </code></pre>