title: Accessing an object's methods while using List Iterator
body: <p>My objective is to iterate over a list of objects, and have the two functionalities:
  - be able to remove from the list during iteration.
  - be able to access the public get methods of the objects I'm iterating over, to be able to determine whether or not they should be removed.</p>
  
  <p>For example, how would I get the following to work? Currently it gives the exception <code>java.util.ArrayList$Itr cannot be cast to random.folder.dir.TestClass</code>.</p>
  
  <pre><code>public class TestClass {
      public int foo;
  
      public TestClass(int foo) {
          this.foo = foo;
      }
  
      public int getFoo() {
          return foo;
      }       
  }
  
  
  
  
  List&lt;TestClass&gt; testList = new ArrayList&lt;TestClass&gt;();
  testList.add(new TestClass(1));
  testList.add(new TestClass(2));
  testList.add(new TestClass(3));
  
  Iterator&lt;TestClass&gt; it = tickScratch.iterator();
  while(it.hasNext()) {
      if(((TestClass)it).getFoo() == 2)
          it.remove();
  }
  </code></pre>