I am going to have a class that has a ArrayList people field I want clients of this class to be able to iterate over those objects. How do I do this? Do I just return people.iterator(); ?
for example, in the client I want to be able to do this:
for( Person p : people ) {
// do something
}
Is this what I need in my class?
public class People implements Iterable<Person> {
private ArrayList<Person> people;
@Override
public Iterator<Person> iterator() {
// TODO Auto-generated method stub
return people.iterator();
}
Iteratorfrom the privateArrayList.