Question

I finished the assignment that I was working on. But i'm trying to modify it a little

What I'm trying to do is remove a record and I want ot use an iterator to do it. if I was using a for each loop it would be something like this.

for(People someone : person)
    (someone instanceof Parent)
        ((Parent)someone.whatever

How would you do that with an iterator?

thanks in advance

Was it helpful?

Solution

I think this is what you are looking for:

public static void main(String[] args) {
    List<Parent> myList = new ArrayList<Parent>();
    Iterator<Parent> iterator = myList.iterator();
    while (iterator.hasNext()) {
        Parent p = iterator.next();
        if (p instanceof Child) {
          //do something with the Child
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top