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.
For example, how would I get the following to work? Currently it gives the exception java.util.ArrayList$Itr cannot be cast to random.folder.dir.TestClass.
public class TestClass {
public int foo;
public TestClass(int foo) {
this.foo = foo;
}
public int getFoo() {
return foo;
}
}
List<TestClass> testList = new ArrayList<TestClass>();
testList.add(new TestClass(1));
testList.add(new TestClass(2));
testList.add(new TestClass(3));
Iterator<TestClass> it = tickScratch.iterator();
while(it.hasNext()) {
if(((TestClass)it).getFoo() == 2)
it.remove();
}